(phixonline)-->
with javascript_semantics
include mpfr.e
function juggler(integer n, bool bDigits=false)
atom t0 = time(), t1 = time()+1
assert(n>=1)
mpz a = mpz_init(n),
h = mpz_init(n)
integer l = 0,
hi = 0
while mpz_cmp_si(a,1)!=0 do
if mpz_odd(a) then
mpz_pow_ui(a,a,3)
end if
mpz_sqrt(a,a)
l += 1
if mpz_cmp(a,h)>0 then
mpz_set(h,a)
hi = l
end if
if platform()!=JS and time()>t1 then
progress("working (l=%d)\r",{l})
t1 = time()+1
end if
end while
atom hd = iff(bDigits?mpz_sizeinbase(h,10):mpz_get_atom(h))
t0 = time()-t0
string t = iff(t0>=1?" ("&elapsed(t0)&")":"")
return {n, l, hd, hi, t}
end function
procedure main()
atom t0 = time()
printf(1," n l[n] h[n] i[n]\n")
printf(1,"-----------------------------------\n")
for n=20 to 39 do
printf(1,"%2d %2d %,18d %2d %s\n", juggler(n))
end for
sequence nums = {113, 173, 193, 2183, 11229, 15065, 15845, 30817}
-- alas mpz_sqrt() throws a wobbly over the rest:
-- 48443, 275485, 1267909, 2264915, 5812827, 7110201}
printf(1,"\n n l[n] d[n] i[n]\n")
printf(1,"-----------------------------\n")
-- ... and pwa/p2js(/mpfr.js) only copes with the first 3
for i=1 to iff(platform()=JS?3:length(nums)) do
printf(1,"%9d %3d %,6d %2d %s\n", juggler(nums[i],true))
end for
?elapsed(time()-t0)
end procedure
main()