Goodstein Sequence: Difference between revisions
Content deleted Content added
typo |
m →{{header|Julia}}: Using a generic vector instead of an inheritance tree of elements is faster here, for once. |
||
Line 54: | Line 54: | ||
=={{header|Julia}}== |
=={{header|Julia}}== |
||
<syntaxhighlight lang="julia"> |
<syntaxhighlight lang="julia">""" Given nonnegative integer n and base b, return hereditary representation consisting of |
||
⚫ | |||
⚫ | |||
""" |
|||
end |
|||
⚫ | |||
⚫ | |||
abstract type HereditaryRepresentation end |
|||
⚫ | |||
⚫ | |||
struct HRTuple <: HereditaryRepresentation |
|||
decomp = Vector{Union{typeof(n), Vector}}[] |
|||
mult::Int |
|||
e = typeof(n)(0) |
|||
exp::GBase |
|||
end |
|||
struct HRVector <: HereditaryRepresentation |
|||
mult::Int |
|||
exp::Vector{HereditaryRepresentation} |
|||
end |
|||
⚫ | |||
⚫ | |||
⚫ | |||
decomp = HereditaryRepresentation[] |
|||
while n != 0 |
while n != 0 |
||
n, r = divrem(n, |
n, r = divrem(n, b) |
||
if r |
if r > 0 |
||
push!(decomp, [r, decompose(e, b)]) |
|||
push!(decomp, HRVector(r, decompose(e, bas))) |
|||
elseif e == bas.b |
|||
push!(decomp, HRTuple(r, bas)) |
|||
end |
|||
⚫ | |||
push!(decomp, HRTuple(r, GBase(e))) |
|||
end |
end |
||
e += 1 |
e += 1 |
||
Line 90: | Line 73: | ||
end |
end |
||
""" Evaluate hereditary representation d under base b """ |
|||
evaluate(i::Integer, _) = i |
|||
evaluate(d, b) = d isa Integer ? d : sum(j * b ^ evaluate(k, b) for (j, k) in d) |
|||
evaluate(bas::GBase, _) = bas.b |
|||
evaluate(t::HRTuple, bas) = t.mult * (bas.b)^(evaluate(t.exp, bas)) |
|||
evaluate(hv::HRVector, bas) = hv.mult * bas.b^evaluate(hv.exp, bas) |
|||
evaluate(arr::Vector{HereditaryRepresentation}, bas) = isempty(arr) ? 0 : sum(evaluate(a, bas) for a in arr) |
|||
""" 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 """ |
||
function goodstein(n |
function goodstein(n, limitlength = 10) |
||
seq = typeof(n)[] |
|||
b = typeof(n)(2) |
|||
while |
while length(seq) < limitlength |
||
push!( |
push!(seq, n) |
||
n == 0 && break |
|||
d = decompose(n, b) |
|||
bas.b += 1 # Since bas is a reference in d this increments all GBase subcomponents of d |
|||
b += 1 |
|||
n = evaluate(d, b) - 1 |
|||
end |
end |
||
return |
return seq |
||
end |
end |
||
""" |
"""Get the Nth term of Goodstein(n) sequence counting from 0, see https://oeis.org/A266201""" |
||
A266201(n) = last(goodstein(BigInt(n), |
A266201(n) = last(goodstein(BigInt(n), n + 1)) |
||
println("Goodstein(n) sequence (first 10) for values of n from 0 through 7:") |
println("Goodstein(n) sequence (first 10) for values of n from 0 through 7:") |
||
for i in |
for i in 1:7 |
||
println("Goodstein of $i: |
println("Goodstein of $i: $(goodstein(i))") |
||
end |
end |
||
println("\nThe Nth term of Goodstein(N) sequence counting from 0, for values of N from 0 through 16:") |
println("\nThe Nth term of Goodstein(N) sequence counting from 0, for values of N from 0 through 16:") |
||
for i in big" |
for i in big"1":16 |
||
println("Term $i of Goodstein($i): |
println("Term $i of Goodstein($i}): $(A266201(i))") |
||
end |
end |
||
</syntaxhighlight>{{out}} |
</syntaxhighlight>{{out}} |