Hamming numbers: Difference between revisions
→Infinite generator, avoiding duplicates, using logarithms for faster processing: Julia: improved algorithm, tweaked performance...
GordonBGood (talk | contribs) (→Fast enumerating logarithmic version: C# - imperative log improved algorithm...) |
GordonBGood (talk | contribs) (→Infinite generator, avoiding duplicates, using logarithms for faster processing: Julia: improved algorithm, tweaked performance...) |
||
Line 6,358:
===Infinite generator, avoiding duplicates, using logarithms for faster processing===
The above code is slow for several reasons, partly because it is doing many multi-precision integer multiplications requiring much memory allocation and garbage collection for which
{{trans|Nim}}
<syntaxhighlight lang="julia">
BigInt(2)^twos * BigInt(3)^threes * BigInt(5)^fives▼
lg :: Float64
x2 :: UInt32
x3 :: UInt32
x5 :: UInt32
const ONE = LogRep(0.0, 0, 0, 0)
const LB2_2 = 1.0
const LB2_3 = log(2,3)
const LB2_5 = log(2,5)
function mult2(lr :: LogRep) # :: LogRep
LogRep(lr.lg + LB2_2, lr.x2 + 1, lr.x3, lr.x5)
function mult3(lr :: LogRep) # :: LogRep
LogRep(lr.lg + LB2_3, lr.x2, lr.x3 + 1, lr.x5)
end
function mult5(lr :: LogRep) # :: LogRep
LogRep(lr.lg + LB2_5, lr.x2, lr.x3, lr.x5 + 1)
end
function lr2BigInt(lr :: LogRep) # :: BigInt
end
mutable struct
HammingsLog() = new(
[ONE],
mult5(ONE),
mult3(ONE),
1, 1
)
end
Base.eltype(::Type{
function Base.iterate(HM::
if st.
resize!(st.
end
rslt = @inbounds(st.s2[st.s2hdi])
if rslt.lg < st.mrg.lg
st.s2hdi += 1
else
if st.
resize!(st.
end
rslt = st.mrg; push!(st.s3, mult3(rslt))
st.s3hdi += 1; chkv = @inbounds(st.s3[st.s3hdi])
if chkv.lg < st.s5.lg
else
st.
end
end
push!(st.s2, mult2(rslt)); rslt, st
▲ len53 = length(st.ham53)
end
▲ nlen53 = len53 - st.ndx53 + 1
function test(n :: Int) :: Tuple{LogRep, Float64}
start =
▲ end
elpsd = (time() - start) * 1000
rslt, elpsd
▲ end
end
foreach(x -> print(
let count = 1691; for t in
rslt, elpsd = test(1000000)
▲let count = 1000000; for t in Hammings() count <= 1 && (println(trival(t)); break); count -= 1 end end</syntaxhighlight>
println(lr2BigInt(rslt))
println("This last took $elpsd milliseconds.")</syntaxhighlight>
{{out}}
<pre>1 2 3 4 5 6 8 9 10 12 15 16 18 20 24 25 27 30 32 36
2125764000
519312780448388736089589843750000000000000000000000000000000000000000000000000000000
This last took 16.8759822845459 milliseconds.</pre>
The above execution time is as run on an Intel i5-6500 at 3.6 GHz (single threaded boost), and the program can find the billionth Hamming number in about 17 seconds.
===Determination of the nth Hamming number by processing of error band===
|