Quantcast
Viewing all articles
Browse latest Browse all 28

Answer by tamir for Sieve of Eratosthenes - Finding Primes Python

I just came up with this. It may not be the fastest, but I'm not using anything other than straight additions and comparisons. Of course, what stops you here is the recursion limit.

def nondivsby2():    j = 1    while True:        j += 2        yield jdef nondivsbyk(k, nondivs):    j = 0    for i in nondivs:        while j < i:            j += k        if j > i:            yield idef primes():    nd = nondivsby2()    while True:        p = next(nd)        nd = nondivsbyk(p, nd)        yield pdef main():    for p in primes():        print(p)

Viewing all articles
Browse latest Browse all 28


<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>