On 09/05/2017 13:56, Robert L. wrote:
Charles Babbage, looking ahead to the sorts of problems his
Analytical Engine would be able to solve, gave this example:

What is the smallest positive integer whose square ends in the
digits 269,696? -- Babbage, letter to Lord Bowden, 1837; see
Hollingdale and Tootill, Electronic Computers, second edition,
1970, p. 125.

He thought the answer might be 99,736, whose square is
9,947,269,696; but he couldn't be certain.

Task

The task is to find out if Babbage had the right answer -- and
to do so, as far as your language allows it, in code that
Babbage himself would have been able to read and understand.


(use srfi-121)

;; Create function that accepts a number and returns
;; true if, only if, it satisfies the requirements.
(define (good? n) (equal? 269696 (modulo (* n n) 1000000)))

;; A generator for the natural numbers (1,2,3,4 ...).
;; Each time it is called, it yields the next number.
(define generate-naturals (make-range-generator 1))

(generator-find good? generate-naturals)

 ===>
25264

-----

(use lazy-lists)

(define (good? n) (equal? 269696 (modulo (* n n) 1000000)))

(First (Filter good? (Cardinals)))

 ===>
25264


This time I know this is not Python! This is easy to solve by brute-force as we know the answer is small:

i=0
while 1:
    if (i*i) % 1000000 == 269696:
        print ("Found:",i)
        break
    i+=1

I think Babbage would have had a better chance of understanding this than whatever was used above.

(There was a short-lived language actually called 'Babbage', which I once implemented, although I doubt the man himself would have made much of it. But he might have been p*ssed off that it wasn't quite as successful as 'Ada', named after his programmer.)

--
bartc
--
https://mail.python.org/mailman/listinfo/python-list

Reply via email to