Goodstein Sequence: Difference between revisions

Content deleted Content added
Wherrera (talk | contribs)
m To avoid needing big integer types the Goodstein(n)(n) task has to have n < 11.
PureFox (talk | contribs)
→‎{{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 "./dynamic" for Tuple, Struct
<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"])
// consisting of tuples (j, k) so sum of all (j * b^(evaluate(k, b)) = n.

var HR = Tuple.create("HR", ["mult", "exp"])

// Given integer n and base b
// return tuples (j, k) so sum of all (j * b^(evaluate(k)) = n.
var decompose // recursive
var decompose // recursive
decompose = Fn.new { |n, bas|
decompose = Fn.new { |n, b|
var e = 0
if (n < b) return n
var decomp = []
var decomp = []
var e = BigInt.zero
while (n != 0) {
while (n != 0) {
var t = (n is Num) ? (n/bas.b).truncate : n/bas.b
var t = n.divMod(b)
var r = n % bas.b
n = t[0]
n = t
var r = t[1]
if (r != 0 && e >= bas.b) {
if (r > 0) decomp.add([r, decompose.call(e, b)])
if (e > bas.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 { |p, bas|
evaluate = Fn.new { |d, b|
if (p is Num || p is BigInt) return p
if (d is BigInt) return d
if (p is GBase) return p.b
var sum = BigInt.zero
if (p is HR) {
for (a in d) {
var e = evaluate.call(p.exp, bas)
var j = a[0]
if (e is BigInt) e = e.toSmall
var k = a[1]
if (p.mult is Num) {
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
var sum = (p is Num) ? 0 : BigInt.zero
for (a in p) sum = sum + evaluate.call(a, bas)
return sum
}
}
return sum
}
}


// 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 sequence = []
var seq = []
var bas = GBase.new(2)
var b = BigInt.two
while (n >= 0 && sequence.count < limitLength) {
while (seq.count < limitLength) {
sequence.add(n)
seq.add(n)
var d = decompose.call(n, bas)
if (n == 0) break
bas.b = bas.b + 1
var d = decompose.call(n, b)
n = evaluate.call(d, bas) - 1
b = b.inc
n = evaluate.call(d, b) - 1
}
}
return sequence
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(BigInt.new(n), n + 1)[-1] }
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 0..7) System.print("Goodstein of %(i): %(goodstein.call(i, 10))")
for (i in BigInt.zero..7) System.print("Goodstein of %(i): %(goodstein.call(i, 10))")


System.print("\nThe Nth term of Goodstein(N) sequence counting from 0, for values of N in [0, 16]:")
System.print("\nThe nth term of Goodstein(N) sequence counting from 0, for values of n in [0, 16]:")
for (i in 0..16) {
for (i in BigInt.zero..16) {
Fmt.print("Term $2d of Goodstein($2d): $i", i, i, a266201.call(i, 10))
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 Nth term of Goodstein(N) sequence counting from 0, for values of N in [0, 16]:
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