RosettaCodeData/Task/Iterated-digits-squaring/Phix/iterated-digits-squaring-1.phix
2026-02-01 16:33:20 -08:00

42 lines
1.2 KiB
Text

with javascript_semantics
constant MAXINT = power(2,iff(machine_bits()=32?53:64))
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, i1= i+1, i1ms = i1-s
if s>i then exit end if
sums[i1] += sums[i1ms]
end for
end for
atom count89 = 0
for i=1 to n*81 do
integer r, digit, w = i, i1 = i+1
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[i1]
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