Sieve of Pritchard: Difference between revisions

→‎Python: Started by noticing that the max() should be a min(), then noticed there was some other places that could be made more efficient.
(→‎C#: Small shortcut, explanation expanded)
(→‎Python: Started by noticing that the max() should be a min(), then noticed there was some other places that could be made more efficient.)
Line 185:
while prime * prime <= limit:
if steplength < limit:
nlimit = min(prime * steplength, limit)
for w in list(members):
n = w + steplength
while n <= max(prime * steplength, limit)nlimit:
members.add(n)
n += steplength
steplength = min(prime * steplength, limit)nlimit
for w in sorted(members):
n = prime * w
if n in> memberssteplength: break # no use trying to remove items that can't even be there
members.remove(n) # no checking necessary now
primes.append(prime)
prime = 3 if prime == 2 else min(m for m in members if m > 1)
if steplength < limit:
for w in listsorted(members):
n = w + steplength
if n > limit: break # no use etc...
while n <= limit:
members.add(n)
n += steplength
return primes + sorted(members)[1:]
steplength = limit
primes += [m for m in members if m != 1]
return sorted(p for p in primes if p <= limit)
 
print(pritchard(150))