Goodstein Sequence: Difference between revisions
Content deleted Content added
m To avoid needing big integer types the Goodstein(n)(n) task has to have n < 11. |
→{{header|Wren}}: Updated in line with Julia example of which it is a translation. |
||
Line 219: | Line 219: | ||
=={{header|Wren}}== |
=={{header|Wren}}== |
||
{{trans|Julia}} |
{{trans|Julia}} |
||
{{libheader|Wren-dynamic}} |
|||
{{libheader|Wren-big}} |
{{libheader|Wren-big}} |
||
{{libheader|Wren-fmt}} |
{{libheader|Wren-fmt}} |
||
<syntaxhighlight lang="wren">import "./ |
<syntaxhighlight lang="wren">import "./big" for BigInt |
||
import "./big" for BigInt |
|||
import "./fmt" for Fmt |
import "./fmt" for Fmt |
||
// Given non-negative integer n and base b, return hereditary representation |
|||
var GBase = Struct.create("GBase", ["b"]) |
|||
⚫ | |||
var HR = Tuple.create("HR", ["mult", "exp"]) |
|||
// Given integer n and base b |
|||
⚫ | |||
var decompose // recursive |
var decompose // recursive |
||
decompose = Fn.new { |n, |
decompose = Fn.new { |n, b| |
||
if (n < b) return n |
|||
var decomp = [] |
var decomp = [] |
||
⚫ | |||
while (n != 0) { |
while (n != 0) { |
||
var t = |
var t = n.divMod(b) |
||
n = t[0] |
|||
var r = t[1] |
|||
if (r |
if (r > 0) decomp.add([r, decompose.call(e, b)]) |
||
e = e.inc |
|||
decomp.add(HR.new(r, decompose.call(e, bas))) |
|||
} else if (e == bas.b) { |
|||
decomp.add(HR.new(r, bas)) |
|||
} |
|||
} else { |
|||
decomp.add(HR.new(r, GBase.new(e))) |
|||
} |
|||
e = e + 1 |
|||
} |
} |
||
return decomp |
return decomp |
||
} |
} |
||
// Evaluate hereditary representation d under base b. |
|||
var evaluate // recursive |
var evaluate // recursive |
||
evaluate = Fn.new { | |
evaluate = Fn.new { |d, b| |
||
if ( |
if (d is BigInt) return d |
||
var sum = BigInt.zero |
|||
for (a in d) { |
|||
var |
var j = a[0] |
||
var k = a[1] |
|||
sum = sum + j * b.pow(evaluate.call(k, b)) |
|||
return p.mult * (bas.b).pow(e) |
|||
} else { |
|||
return p.mult * BigInt.new(bas.b).pow(e) |
|||
} |
|||
} |
|||
if (p is List) { |
|||
if (p.count == 0) return 0 |
|||
⚫ | |||
⚫ | |||
⚫ | |||
} |
} |
||
⚫ | |||
} |
} |
||
// Return a vector of up to limitlength values of the Goodstein sequence for n. |
// Return a vector of up to limitlength values of the Goodstein sequence for n. |
||
var goodstein = Fn.new { |n, limitLength| |
var goodstein = Fn.new { |n, limitLength| |
||
var |
var seq = [] |
||
var |
var b = BigInt.two |
||
while ( |
while (seq.count < limitLength) { |
||
seq.add(n) |
|||
if (n == 0) break |
|||
var d = decompose.call(n, b) |
|||
b = b.inc |
|||
⚫ | |||
} |
} |
||
return |
return seq |
||
} |
} |
||
// Get the nth term of the Goodstein(n) sequence counting from 0 |
// Get the nth term of the Goodstein(n) sequence counting from 0 |
||
var a266201 = Fn.new { |n| goodstein.call |
var a266201 = Fn.new { |n| goodstein.call(n, (n + 1).toSmall)[-1] } |
||
System.print("Goodstein(n) sequence (first 10) for values of n in [0, 7]:") |
System.print("Goodstein(n) sequence (first 10) for values of n in [0, 7]:") |
||
for (i in |
for (i in BigInt.zero..7) System.print("Goodstein of %(i): %(goodstein.call(i, 10))") |
||
System.print("\nThe |
System.print("\nThe nth term of Goodstein(N) sequence counting from 0, for values of n in [0, 16]:") |
||
for (i in |
for (i in BigInt.zero..16) { |
||
Fmt.print("Term $ |
Fmt.print("Term $2i of Goodstein($2i): $i", i, i, a266201.call(i, 10)) |
||
}</syntaxhighlight> |
}</syntaxhighlight> |
||
Line 311: | Line 291: | ||
Goodstein of 7: [7, 30, 259, 3127, 46657, 823543, 16777215, 37665879, 77777775, 150051213] |
Goodstein of 7: [7, 30, 259, 3127, 46657, 823543, 16777215, 37665879, 77777775, 150051213] |
||
The |
The nth term of Goodstein(N) sequence counting from 0, for values of n in [0, 16]: |
||
Term 0 of Goodstein( 0): 0 |
Term 0 of Goodstein( 0): 0 |
||
Term 1 of Goodstein( 1): 0 |
Term 1 of Goodstein( 1): 0 |