Goodstein Sequence: Difference between revisions

→‎{{header|Wren}}: Updated in line with Julia example of which it is a translation.
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:
=={{header|Wren}}==
{{trans|Julia}}
{{libheader|Wren-dynamic}}
{{libheader|Wren-big}}
{{libheader|Wren-fmt}}
<syntaxhighlight lang="wren">import "./dynamicbig" for Tuple, StructBigInt
import "./big" for BigInt
import "./fmt" for Fmt
 
// Given non-negative integer n and base b, return hereditary representation
var GBase = Struct.create("GBase", ["b"])
// returnconsisting 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
decompose = Fn.new { |n, basb|
varif e(n =< 0b) return n
var decomp = []
var sume = (p is Num) ? 0 : BigInt.zero
while (n != 0) {
var t = (n is Num) ? .divMod(n/bas.b).truncate : n/bas.b
var rn = n % bas.bt[0]
nvar r = t[1]
if (r !=> 0) &&decomp.add([r, decompose.call(e, >= bas.b) {])
e = if (e > bas.b) {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
}
 
// Evaluate hereditary representation d under base b.
var evaluate // recursive
evaluate = Fn.new { |pd, basb|
if (p is Num || pd is BigInt) return pd
ifvar (psum is= GBase) return pBigInt.bzero
iffor (pa isin HRd) {
var ej = evaluate.call(p.exp, bas)a[0]
ifvar (e is BigInt) ek = e.toSmalla[1]
ifsum = sum + j * b.pow(pevaluate.multcall(k, is Numb)) {
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.
var goodstein = Fn.new { |n, limitLength|
var sequenceseq = []
var basb = GBaseBigInt.new(2)two
while (n >= 0 && sequenceseq.count < limitLength) {
sequenceseq.add(n)
varif d = decompose.call(n, bas== 0) break
bas.bvar d = basdecompose.call(n, b + 1)
nb = evaluateb.call(d, bas) - 1inc
for (a in p) sumn = sum + evaluate.call(ad, basb) - 1
}
return sequenceseq
}
 
// Get the nth term of the Goodstein(n) sequence counting from 0
var a266201 = Fn.new { |n| goodstein.call(BigInt.new(n), (n + 1).toSmall)[-1] }
 
System.print("Goodstein(n) sequence (first 10) for values of n in [0, 7]:")
for (i in 0BigInt.zero..7) System.print("Goodstein of %(i): %(goodstein.call(i, 10))")
 
System.print("\nThe Nthnth term of Goodstein(N) sequence counting from 0, for values of Nn in [0, 16]:")
for (i in 0BigInt.zero..16) {
Fmt.print("Term $2d2i of Goodstein($2d2i): $i", i, i, a266201.call(i, 10))
}</syntaxhighlight>
 
Line 311 ⟶ 291:
Goodstein of 7: [7, 30, 259, 3127, 46657, 823543, 16777215, 37665879, 77777775, 150051213]
 
The Nthnth term of Goodstein(N) sequence counting from 0, for values of Nn in [0, 16]:
Term 0 of Goodstein( 0): 0
Term 1 of Goodstein( 1): 0
9,493

edits