Jump to content

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 ===
and always planned to write a gmp version, but now that just feels like too much effort for too little gain.
<!--(phixonline)-->
<syntaxhighlight lang="phix">
with javascript_semantics
function digits(atom n, b)
-- least significant first, eg 123,10 -> {3,2,1} or 6,2 -> {0,1,1}
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
forwhile i,d in digits(n,b) do
rinteger d &= remainder(n,b)
if d then
res += d*round(power(b+1,bump(i-1,b)))
end if
n = floor(n/b)
end for
i += 1
end while
return res
end function
 
function goodstein(atominteger n, maxterms = 10)
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: {0}
Goodstein of 1: {1, 0}
Goodstein of 2: {2, 2, 1, 0}
Goodstein of 3: {3, 3, 3, 2, 1, 0}
Goodstein of 4: {4, 26, 41, 60, 83, 109, 139, 173, 211, 253}
Goodstein of 5: {5, 27, 255, 467, 775, 1197, 1751, 2454, 3325, 4382}
Goodstein of 6: {6, 29, 257, 3125, 46655, 98039, 187243, 332147, 555551, 885775}
Goodstein of 7: {7, 30, 259, 3127, 46657, 823543, 16777215, 37665879, 77777775, 150051213}
 
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
function digitsbump(atommpz npn, integer b)
mpz {n, tmp, res, i} = mpz_inits(4)
mpz_set(n,pn)
while mpz_cmp_si(n>=b,0) do
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
return rres
end function
 
function goodstein(integer n, maxterms = 10)
sequence rres = {remaindermpz_init(n,b)}
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),", ")})
end for
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>
 
7,833

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.