Goodstein Sequence: Difference between revisions
m
→{{header|Phix}}: simplified, added gmp version
m (→{{header|Phix}}: simplified, added gmp version) |
|||
Line 229:
=={{header|Phix}}==
Modified version of the python code from A059934 - tbh, I did not expect to get anywhere near this far using native atoms
=== using native atoms ===
<!--(phixonline)-->
<syntaxhighlight lang="phix">
with javascript_semantics
function digits(atom n, b)▼
sequence r = {remainder(n,b)}▼
while n>=b do▼
n = floor(n/b)▼
r &= remainder(n,b)▼
end while▼
return r▼
end function▼
function bump(atom n, b)
atom res = 0, i = 0
if d then
res += d*round(power(b+1,bump(i
end if
▲ n = floor(n/b)
end for▼
i += 1
▲ end while
return res
end function
function goodstein(
sequence res = {n}
while length(res)<maxterms and res[$]!=0 do
Line 278 ⟶ 271:
<pre>
Goodstein(n) sequence (first 10) for values of n from 0 through 7:
Goodstein of 0:
Goodstein of 1:
Goodstein of 2:
Goodstein of 3:
Goodstein of 4:
Goodstein of 5:
Goodstein of 6:
Goodstein of 7:
The Nth term of Goodstein(N) sequence counting from 0, for values of N from 0 through 16:
Line 305 ⟶ 298:
Term 15 of Goodstein(15): 14063084452070776847260 (inaccurate)
Term 16 of Goodstein(16): 27715173799965170860...62604488626682848248 (862 digits) (inaccurate)
</pre>
=== gmp version ===
<syntaxhighlight lang="phix">
include mpfr.e
mpz {n, tmp, res, i} = mpz_inits(4)
mpz_set(n,pn)
integer d = mpz_fdiv_q_ui(n,n,b)
if d then
integer bib = mpz_get_integer(bump(i,b))
mpz_ui_pow_ui(tmp,b+1,bib)
mpz_mul_si(tmp,tmp,d)
mpz_add(res,res,tmp)
end if
mpz_add_ui(i,i,1)
end while
▲end function
function goodstein(integer n, maxterms = 10)
while length(res)<maxterms and mpz_cmp_si(res[$],0)!=0 do
mpz tmp = bump(res[$],length(res)+1)
mpz_sub_si(tmp,tmp,1)
res &= tmp
end while
return res
end function
printf(1,"Goodstein(n) sequence (first 10) for values of n from 0 through 7:\n")
for i=0 to 7 do
printf(1,"Goodstein of %d: %s\n",{i,join(apply(goodstein(i),mpz_get_str),", ")})
printf(1,"\n")
printf(1,"The Nth term of Goodstein(N) sequence counting from 0, for values of N from 0 through 16:\n")
for i=0 to 16 do
string gs = mpz_get_short_str(goodstein(i,i+1)[$])
printf(1,"Term %d of Goodstein(%d): %s\n",{i,i,gs})
end for
</syntaxhighlight>
{{out}}
As above, except ending with
<pre>
Term 13 of Goodstein(13): 6568408355712901455
Term 14 of Goodstein(14): 295147905179358418247
Term 15 of Goodstein(15): 14063084452070776884879
Term 16 of Goodstein(16): 27715173799965169706...68941004114162614925 (862 digits)
</pre>
|