Data commit

This commit is contained in:
Ingy döt Net 2023-07-01 11:58:00 -04:00
parent 7387c8f97b
commit cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions

View file

@ -0,0 +1,11 @@
function hailstonelength(n::Integer)
len = 1
while n > 1
n = ifelse(iseven(n), n ÷ 2, 3n + 1)
len += 1
end
return len
end
@show hailstonelength(27); nothing
@show findmax([hailstonelength(i) for i in 1:100_000]); nothing

View file

@ -0,0 +1,39 @@
struct HailstoneSeq{T<:Integer}
count::T
end
Base.eltype(::HailstoneSeq{T}) where T = T
function Base.iterate(h::HailstoneSeq, state=h.count)
if state == 1
(1, 0)
elseif state < 1
nothing
elseif iseven(state)
(state, state ÷ 2)
elseif isodd(state)
(state, 3state + 1)
end
end
function Base.length(h::HailstoneSeq)
len = 0
for _ in h
len += 1
end
return len
end
function Base.show(io::IO, h::HailstoneSeq)
f5 = collect(Iterators.take(h, 5))
print(io, "HailstoneSeq{", join(f5, ", "), "...}")
end
hs = HailstoneSeq(27)
println("Collection of the Hailstone sequence from 27: $hs")
cl = collect(hs)
println("First 5 elements: ", join(cl[1:5], ", "))
println("Last 5 elements: ", join(cl[end-4:end], ", "))
Base.isless(h::HailstoneSeq, s::HailstoneSeq) = length(h) < length(s)
println("The number with the longest sequence under 100,000 is: ", maximum(HailstoneSeq.(1:100_000)))

View file

@ -0,0 +1,40 @@
struct HailstoneSeq{T<:Integer}
start::T
end
Base.eltype(::HailstoneSeq{T}) where T = T
Base.start(hs::HailstoneSeq) = (-1, hs.start)
Base.done(::HailstoneSeq, state) = state == (1, 4)
function Base.next(::HailstoneSeq, state)
_, s2 = state
s1 = s2
if iseven(s2)
s2 = s2 ÷ 2
else
s2 = 3s2 + 1
end
return s1, (s1, s2)
end
function Base.length(hs::HailstoneSeq)
r = 0
for _ in hs
r += 1
end
return r
end
function Base.show(io::IO, hs::HailstoneSeq)
f5 = collect(Iterators.take(hs, 5))
print(io, "HailstoneSeq(", join(f5, ", "), "...)")
end
hs = HailstoneSeq(27)
println("Collection of the Hailstone sequence from 27: $hs")
cl = collect(hs)
println("First 5 elements: ", join(cl[1:5], ", "))
println("Last 5 elements: ", join(cl[end-4:end], ", "))
Base.isless(h::HailstoneSeq, s::HailstoneSeq) = length(h) < length(s)
println("The number with the longest sequence under 100,000 is: ", maximum(HailstoneSeq.(1:100_000)))