Sieve of Eratosthenes: Difference between revisions

→‎{{header|Nim Unbounded Versions}}: fix index and edge case errors...
m (→‎{{header|Nim Unbounded Versions}}: minor edit to conform to Extensivle Generator requirements...)
(→‎{{header|Nim Unbounded Versions}}: fix index and edge case errors...)
Line 8,164:
let size = ((reqdsize.ashr 12) + 1) shl 9 # size in bytes
if size > cmpsts.len: cmpsts = newSeq[byte](size)
for i in countup(0, cmpsts.len, 512):- copySeq(cmpsts, i, bpptrn, 01, 512):
copySeq(cmpsts, i, bpptrn, 0, 512)
sieveComposites(low, cmpsts, bpas)
let arr = composites2BasePrimeArray(low, cmpsts)
Line 8,197 ⟶ 8,198:
let size = ((reqdsize.ashr 17) + 1) shl 14 # size in bytes
if size > cmpsts.len: cmpsts = newSeq[byte](size)
for i in countup(0, cmpsts.len, 16384):- copySeq(cmpsts, i, pgptrn, 01, 16384):
copySeq(cmpsts, i, pgptrn, 0, 16384)
sieveComposites(low, cmpsts, bpas)
next += cmpsts.len.Prime shl 4
Line 8,203 ⟶ 8,205:
 
proc countPrimesTo(range: Prime): int64 =
if range < 3: return (if range < 2: 0 else: 1 )
var count = 1
for low, cmpsts in makePrimePages(): # almost never exits!!!
if low + (cmpsts.len shl 4).Prime > range:
let lasti = ((range - low).int.ashr shr 1).int
count += countComposites(cmpsts[0..<(lasti.ashr shr(3) and -32)]) # by 32 bytes!
echo count, +=" 8", -lasti, " ", (cmpsts[lasti.ashr shr 3] or
for i in (lasti.shr(3).and -32)..<(lasti shr 3): # last whole bytes!
(0xFE'u8 shl (lasti and 7))).int32.countBits32
count += 8 - cmpsts[i].and(0xFF).int32.countBits32
count += 8 - (cmpsts[lasti shr 3].int32 or
(0xFE'u8-2i32 shl (lasti and 7))).int32and(0xFF).countBits32
break
count += countComposites(cmpsts)
474

edits