Equal prime and composite sums: Difference between revisions

Content added Content deleted
(Added XPL0 example.)
Line 35: Line 35:
<lang julia>using Primes
<lang julia>using Primes


function getsequencematches(N)
function getsequencematches(N, masksize = 10_000_000_000)
pmask = primesmask(N)
pmask = primesmask(masksize)
psum, csum, pindex, cindex, pcount, ccount = 2, 4, 2, 4, 1, 1
found, psum, csum, pindex, cindex, pcount, ccount = 0, 2, 4, 2, 4, 1, 1
incrementpsum() = (pindex += 1; if pmask[pindex] psum += pindex; pcount += 1 end)
incrementpsum() = (pindex += 1; if pmask[pindex] psum += pindex; pcount += 1 end)
incrementcsum() = (cindex += 1; if !pmask[cindex] csum += cindex; ccount += 1 end)
incrementcsum() = (cindex += 1; if !pmask[cindex] csum += cindex; ccount += 1 end)
while pindex < N
while found < N
while psum < csum
while psum < csum
pindex >= N && return
pindex >= masksize && return
incrementpsum()
incrementpsum()
end
end
if psum == csum
if psum == csum
println("Primes up to $pindex at position $pcount and composites up to $cindex at position $ccount sum to $psum.")
println("Primes up to $pindex at position $pcount and composites up to $cindex at position $ccount sum to $psum.")
found += 1
while psum == csum
while psum == csum
incrementpsum()
incrementpsum()
Line 58: Line 59:
end
end


getsequencematches(500_000_000)
@time getsequencematches(11)
</lang>{{out}}
</lang>{{out}}
<pre>
<pre>
Line 72: Line 73:
Primes up to 86254457 at position 5012372 and composites up to 21123471 at position 19786181 sum to 209456854921713.
Primes up to 86254457 at position 5012372 and composites up to 21123471 at position 19786181 sum to 209456854921713.
Primes up to 390180569 at position 20840220 and composites up to 91491160 at position 86192660 sum to 3950430820867201.
Primes up to 390180569 at position 20840220 and composites up to 91491160 at position 86192660 sum to 3950430820867201.
72.974740 seconds (1.09 G allocations: 19.829 GiB, 0.77% gc time)
</pre>
</pre>