Babbage’s Number
October 9, 2018
This is simple:
(define (babbage1) (let loop ((n 1)) (if (= (modulo (* n n) 1000000) 269696) n (loop (+ n 1)))))
I won’t provide an example; you have to compute it yourself.
Our program runs so quickly it doesn’t seem worth the effort to optimize it, but you could make it faster by only testing numbers whose last digit is 4 or 6, in a manner similar to a prime wheel:
(define (babbage2) (let ((wheel (vector 2 8))) (let loop ((n 4) (w 0)) (if (= (modulo (* n n) 1000000) 269696) n (loop (+ n (vector-ref wheel w)) (- 1 w))))))
You can run the program at https://ideone.com/fKfXXe.
Ruby one-liner.
GTM>w 269696**.5 ; Square root of 269696
519.322635747759401 ; So start with 520.
; Only squares ending in 6 come from numbers ending in 4 or 6.
GTM>f i=520:10:1000000 f j=4,6 s k=i+j,sq=k*k i $e(sq,$l(sq)-5,$l(sq))=269696 w !,k
25264 <– Smallest
99736
150264
224736
275264
349736
400264
474736
525264
599736
650264
724736
775264
849736
900264
974736
Klong version
Examples:
[25264 99736]
Python
25264
99736
150264
224736
275264
349736
400264
474736
525264
599736
650264
724736
775264
849736
900264
974736
Faster Klong version only searching for numbers ending in 4 or 6
Run:
test2()
[25264 99736]
Here’s a solution in Python. The generators yield positive integers whose squares end in 269,696.
Output:
Generalizing the “must end in 4 or 6” argument, we can build up the list of “square roots” digit by digit. It’s also easy to generalize to any radix:
So it’s nice to know that 269696 is b1jp in radix-29, and 241391 squared is 3arpb1jp: