Answer by Paul Gardiner for Sieve of Eratosthenes - Finding Primes Python
I realise this isn't really answering the question of how to generate primes quickly, but perhaps some will find this alternative interesting: because python provides lazy evaluation via generators,...
View ArticleAnswer by Saurabh Rana for Sieve of Eratosthenes - Finding Primes Python
def eratosthenes(n): multiples = [] for i in range(2, n+1): if i not in multiples: print (i) for j in range(i*i, n+1, i): multiples.append(j)eratosthenes(100)
View ArticleAnswer by Pi Delport for Sieve of Eratosthenes - Finding Primes Python
You're not quite implementing the correct algorithm:In your first example, primes_sieve doesn't maintain a list of primality flags to strike/unset (as in the algorithm), but instead resizes a list of...
View ArticleAnswer by Glenn Maynard for Sieve of Eratosthenes - Finding Primes Python
Removing from the beginning of an array (list) requires moving all of the items after it down. That means that removing every element from a list in this way starting from the front is an O(n^2)...
View ArticleSieve of Eratosthenes - Finding Primes Python
Just to clarify, this is not a homework problem :)I wanted to find primes for a math application I am building & came across Sieve of Eratosthenes approach. I have written an implementation of it...
View ArticleAnswer by M.A. for Sieve of Eratosthenes - Finding Primes Python
Empirical Analysis and Visualization of Various Approaches to the Sieve of EratosthenesI discovered this algorithm at the end of the 10th chapter (Maps, Hash Tables, and Skip Lists)of "Data Structures...
View ArticleAnswer by oppressionslayer for Sieve of Eratosthenes - Finding Primes Python
If your looking for even faster, you can use numba and cuda as well ifyou have a Nvidia processor. Feel free to optimize as needed. We use 2**24 which is ~16 million numbers at 239 ms on an Nvidia...
View ArticleAnswer by Andy Richter for Sieve of Eratosthenes - Finding Primes Python
Bitarray and 6k±1 for size and speed1 billion in 5 seconds 2^24 in 3 millisecondsI used bitarray for a smaller footprint. bitarray also allows statements like: p[4::2] = False # Clear multiples of 2The...
View Article