42 lines
1.2 KiB
Text
42 lines
1.2 KiB
Text
constant MAXINT = iff(machine_bits()=32?9007199254740992
|
|
:9223372036854775807)
|
|
|
|
procedure main(integer limit)
|
|
sequence sums = repeat(0,limit*81+1)
|
|
sums[1] = 1
|
|
for n=1 to limit do
|
|
for i=n*81 to 1 by -1 do
|
|
for j=1 to 9 do
|
|
integer s = j*j
|
|
if s>i then exit end if
|
|
sums[i+1] += sums[i+1-s]
|
|
end for
|
|
end for
|
|
atom count89 = 0
|
|
for i=1 to n*81 do
|
|
integer r, digit, w = i
|
|
while w!=1 do
|
|
r = 0
|
|
while w!=0 do
|
|
digit = mod(w,10)
|
|
r += digit*digit
|
|
w = floor(w/10)
|
|
end while
|
|
if r=89 then
|
|
count89 += sums[i+1]
|
|
if count89>MAXINT then
|
|
printf(1,"counter overflow for 10^%d\n",n)
|
|
return
|
|
end if
|
|
exit
|
|
end if
|
|
w = r
|
|
end while
|
|
end for
|
|
printf(1,"There are %d numbers from 1 to 10^%d ending with 89\n",{count89,n})
|
|
end for
|
|
end procedure
|
|
|
|
atom t0 = time()
|
|
main(20)
|
|
?time()-t0
|