Goodstein Sequence: Difference between revisions

m
→‎{{header|Julia}}: Using a generic vector instead of an inheritance tree of elements is faster here, for once.
(typo)
m (→‎{{header|Julia}}: Using a generic vector instead of an inheritance tree of elements is faster here, for once.)
Line 54:
 
=={{header|Julia}}==
<syntaxhighlight lang="julia">mutable""" structGiven GBase{T<:Integer}nonnegative integer n and base b, return hereditary representation consisting of
""" given integer n and base b, return tuples (j, k) sosuch that the sum of all (j * base^(evaluate(k)) = n. """
b::T
"""
end
function decompose(n::T, bas::GBaseb) where T <: Integer
 
eif =n T(0)< b
abstract type HereditaryRepresentation end
elsereturn n
 
b::Tend
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
 
""" given integer n and base b, return tuples (j, k) so sum of all (j * base^(evaluate(k)) = n. """
function decompose(n::T, bas::GBase) where T <: Integer
e = T(0)
decomp = HereditaryRepresentation[]
while n != 0
n, r = divrem(n, bas.b)
if r !=> 0 && e >= bas.b
ifpush!(decomp, e[r, >decompose(e, bas.b)])
push!(decomp, HRVector(r, decompose(e, bas)))
elseif e == bas.b
push!(decomp, HRTuple(r, bas))
end
else
push!(decomp, HRTuple(r, GBase(e)))
end
e += 1
Line 90 ⟶ 73:
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 """
function goodstein(n::T;, limitlength = 10) where T <: Integer
sequenceseq = Ttypeof(n)[]
basb = GBasetypeof(Tn)(2))
while n >= 0 && length(sequenceseq) < limitlength
push!(sequenceseq, n)
dn == decompose(n,0 && bas)break
d = decompose(n, b)
bas.b += 1 # Since bas is a reference in d this increments all GBase subcomponents of d
nb += evaluate(d, bas) - 1
n = evaluate(d, b) - 1
end
return sequenceseq
end
 
""" Get the Nth term of Goodstein(n) sequence counting from 0, see https://oeis.org/A266201 """
A266201(n) = last(goodstein(BigInt(n), limitlength = n + 1))
 
println("Goodstein(n) sequence (first 10) for values of n from 0 through 7:")
for i in 01:7
println("Goodstein of $i: ", $(goodstein(i))")
end
 
println("\nThe Nth term of Goodstein(N) sequence counting from 0, for values of N from 0 through 16:")
for i in big"01":16
println("Term $i of Goodstein($i}): ", $(A266201(i))")
end
</syntaxhighlight>{{out}}
4,111

edits