Goodstein Sequence: Difference between revisions

added Raku programming solution
m (→‎{{header|Phix}}: cut out the middleman, fix a trailing ".0" which had crept in)
(added Raku programming solution)
 
(8 intermediate revisions by 3 users not shown)
Line 52:
 
 
 
=={{header|jq}}==
'''Works with gojq, the Go implementation of jq'''
 
'''Adapted from [[#Wren|Wren]]'''
 
This entry assumes infinite-precision integer arithmetic as provided by the Go
implementation of jq.
<syntaxhighlight lang="jq">
# To take advantage of gojq's infintite-precision integer arithmetic:
def power($b): . as $in | reduce range(0;$b) as $i (1; . * $in);
 
# If $j is 0, then an error condition is raised;
# otherwise, assuming infinite-precision integer arithmetic,
# if the input and $j are integers, then the result will be a pair of integers.
def divmod($j):
. as $i
| ($i % $j) as $mod
| [($i - $mod) / $j, $mod] ;
 
# Given non-negative integer $n and base b$, return hereditary representation
# consisting of tuples (j, k) such that the sum of all (j * b^(evaluate(k; b)) == n.
def decompose($n; $b):
if $n < $b then $n
else { n: $n, decomp: [], e: 0 }
| until (.n == 0;
(.n | divmod($b)) as $t
| .n = $t[0]
| $t[1] as $r
| if $r > 0 then .decomp += [[$r, decompose(.e; $b)]] end
| .e += 1 )
| .decomp
end;
 
# Evaluate hereditary representation $d under base $b.
def evaluate($d; $b):
if $d|type == "number" then $d
else reduce $d[] as [$j, $k] (0;
. + $j * ($b|power(evaluate($k; $b))) )
end ;
 
# Return a vector of up to $limitlength values of the Goodstein sequence for $n.
def goodstein($n; $limitLength):
{ seq: [], b: 2, $n }
| until (.n == false or (.seq|length) >= $limitLength ;
.seq += [.n]
| if .n == 0 then .n = false
else decompose(.n; .b) as $d
| .b += 1
| .n = evaluate($d; .b) - 1
end )
| .seq;
 
# Get the $nth term of the Goodstein($n) sequence counting from 0
def a266201($n): goodstein($n; $n+1)[-1];
 
### The tasks
"Goodstein(n) sequence (first 10) for values of n in [0, 7]:",
(range (0;8) | "Goodstein of \(.): \(goodstein(.; 10))"),
 
"\nThe nth term of Goodstein(n) sequence counting from 0, for values of n in [0, 16]:",
( range (0;17) | "Term \(.) of Goodstein(\(.)): \(a266201(.))" )
</syntaxhighlight>
{{output}}
Command invocation: gojq -n -f goodstein-sequence.jq
<pre>
Goodstein(n) sequence (first 10) for values of n in [0, 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 in [0, 16]:
Term 0 of Goodstein(0): 0
Term 1 of Goodstein(1): 0
Term 2 of Goodstein(2): 1
Term 3 of Goodstein(3): 2
Term 4 of Goodstein(4): 83
Term 5 of Goodstein(5): 1197
Term 6 of Goodstein(6): 187243
Term 7 of Goodstein(7): 37665879
Term 8 of Goodstein(8): 20000000211
Term 9 of Goodstein(9): 855935016215
Term 10 of Goodstein(10): 44580503598539
Term 11 of Goodstein(11): 2120126221988686
Term 12 of Goodstein(12): 155568095557812625
Term 13 of Goodstein(13): 6568408355712901455
Term 14 of Goodstein(14): 295147905179358418247
Term 15 of Goodstein(15): 14063084452070776884879
Term 16 of Goodstein(16): 2771517379996516970665566613559367879596937714713289695169887161862950129194382447127464877388711781205972046374648603545513430106433206876557475731408608398953667881600740852227698037876781766310900319669456854530159244376159780346700931210394158247781113134808720678004134212529413831368888355854503034587880113970541681685966414888841800498150131839091463034162026108960280455620621355407543489960326268155088833218122810217973039385643494213235664908254695964740257569988152978579630435471016976693529875691083071137361386386918409765002837648351746984484967203877495399596876291343126699827442908994036031608979805166915596436929638418152127561722561465793969723556331679336828840983098559789555364076924597258115780567651772009250336359472037679350612341393780002377587368649157608579801815531133644879180066181854487069796160774056572568941004114162614925
</pre>
 
=={{header|Julia}}==
Line 134 ⟶ 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(abs(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
integer 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 170 ⟶ 260:
end for
printf(1,"\n")
integer m64 = machine_bits()=64, maxi = iff(m64?16:15), alim = iff(m64?13:12)
printf(1,"The Nth term of Goodstein(N) sequence counting from 0, for values of N from 0 through 16:\n")
printf(1,"The Nth term of Goodstein(N) sequence counting from 0, for values of N from 0 through %d:\n",maxi)
bool m64 = machine_bits()=64
for i=0 to iff(m64?16:15)maxi do
string ia = iff(i>=iff(m64?13:12)alim?" (inaccurate)":""),
gs = shorten(sprintf("%d",goodstein(i,i+1)[$]))
printf(1,"Term %d of Goodstein(%d): %s%s\n",{i,i,gs,ia})
Line 181 ⟶ 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 208 ⟶ 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 bump(mpz pn, integer b)
mpz {n, tmp, res, i} = mpz_inits(4)
mpz_set(n,pn)
while mpz_cmp_si(n,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 res
end function
 
function goodstein(integer n, maxterms = 10)
sequence res = {mpz_init(n)}
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 17:\n")
for i=0 to 17 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 the last four and one extra term being accurate:
<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)
Term 17 of Goodstein(17): 10685914955539561986...83487458441633279971 (27,776 digits)
</pre>
 
Line 293 ⟶ 432:
Term 16 of Goodstein(16): 2771517379996516970665566613559367879596937714713289695169887161862950129194382447127464877388711781205972046374648603545513430106433206876557475731408608398953667881600740852227698037876781766310900319669456854530159244376159780346700931210394158247781113134808720678004134212529413831368888355854503034587880113970541681685966414888841800498150131839091463034162026108960280455620621355407543489960326268155088833218122810217973039385643494213235664908254695964740257569988152978579630435471016976693529875691083071137361386386918409765002837648351746984484967203877495399596876291343126699827442908994036031608979805166915596436929638418152127561722561465793969723556331679336828840983098559789555364076924597258115780567651772009250336359472037679350612341393780002377587368649157608579801815531133644879180066181854487069796160774056572568941004114162614925
</pre>
 
=={{header|Raku}}==
{{trans|Phix}}
<syntaxhighlight lang="raku" line># 20240219 Raku programming solution
 
sub bump($n is copy, $b) {
loop ( my ($res, $i) = 0, 0; $n.Bool or return $res; $i++ ) {
if my $d = ($n % $b).Int { $res += $d * (($b+1) ** bump($i,$b)).round }
$n = ($n / $b).floor
}
}
 
sub goodstein($n, $maxterms = 10) {
my @res = $n;
while @res.elems < $maxterms && @res[*-1] != 0 {
@res.push(bump(@res[*-1], (@res.elems + 1)) - 1)
}
return @res
}
 
say "Goodstein(n) sequence (first 10) for values of n from 0 through 7:";
for 0..7 -> $i { say "Goodstein of $i: ", goodstein($i) }
 
say();
my $max = 16;
say "The Nth term of Goodstein(N) sequence counting from 0, for values of N from 0 through $max :";
for 0..$max -> $i { say "Term $i of Goodstein($i): {goodstein($i, $i+1)[*-1]}" }</syntaxhighlight>
 
You may [https://ato.pxeger.com/run?1=XZK_btswEMaBjnqKr4YaUJas2ktTWElRdCm6eMpWdPAfyiIqkQ5FtjUMPUmXDM0LZczT9I5SXTkaCIj33Xe_O97vP3b93T88PHpXzt4_v3pq_QYb3xxErKFabM3hmCHeJDhFAGpjDhBojhCxlS1FVIJbzDPMC8Q6_2RMDWNhpfNWgzV0r9IUgwF9quT8eEd5XOQNu-dftMMp6JHecnAKIeJNukgwnQ5AKiNlklvj9Q7d4EYOvc_b4FMSoeVQF3VRxM3sjdm1TipNIuJt1r-ctE1LWYv5AEU4H7kyFdYFX_ysVC3DXS5rSeKbUeLVVYh8nc4W3_Camj93FhIOvq1EAD6rMoiRV4pFkmBGZ89JxzAuFgXs9RGTz2dunaCV917qrYQolW1dQC9pzj_WtSdwU0KjtKYhGFfRgPYVrpeTImLNPM-vMftAz0ATvrTmxFgtMcnGY6In7SFEUkT8VNQ6j-td0ZPdVRIrV4HHwQ7_SVcj0i09k1N6P3BlL3hXL3lDlRFz-L_AvuN69HtRkmiXOI3peSlpb8Lkuwm6frWHDf-36X8B Attempt This Online!]
 
=={{header|Wren}}==
Line 351 ⟶ 519:
for (i in BigInt.zero..7) System.print("Goodstein of %(i): %(goodstein.call(i, 10))")
 
System.print("\nThe nth term of Goodstein(Nn) sequence counting from 0, for values of n in [0, 16]:")
for (i in BigInt.zero..16) {
Fmt.print("Term $2i of Goodstein($2i): $i", i, i, a266201.call(i, 10))
Line 368 ⟶ 536:
Goodstein of 7: [7, 30, 259, 3127, 46657, 823543, 16777215, 37665879, 77777775, 150051213]
 
The nth term of Goodstein(Nn) sequence counting from 0, for values of n in [0, 16]:
Term 0 of Goodstein( 0): 0
Term 1 of Goodstein( 1): 0
354

edits