Answer by РомаТютин for Sieve of Eratosthenes - Finding Primes Python
import mathdef atkin_sieve(limit): primes = [False] * (limit + 1) square_limit = int(math.sqrt(limit))# Отметитьвсечисла, делящиесянацелона 2, 3 или 5for i in range(1, square_limit + 1): for j in...
View ArticleAnswer by Arty for Sieve of Eratosthenes - Finding Primes Python
Thanks for interesting question!Right now I wrote from scratch two versions of classical Sieve of Eratosthenes.One is single-core (CPU core), another one is multi-core (using all CPU cores).Main...
View ArticleAnswer by HoangYell for Sieve of Eratosthenes - Finding Primes Python
here is my solution, the same as Wikipediaimport mathdef sieve_of_eratosthenes(n): a = [i for i in range(2, n+1)] clone_a = a[:] b = [i for i in range(2, int(math.sqrt(n))+1)] for i in b: if i in a: c...
View ArticleAnswer by KevinBui for Sieve of Eratosthenes - Finding Primes Python
Basic sievewith numpy is amazing fast. May be the fastest implementation# record: sieve 1_000_000_000 in 6.9s (core i7 - 2.6Ghz)def sieve_22max_naive(bound): sieve = np.ones(bound, dtype=bool) #...
View ArticleAnswer by pythoteric for Sieve of Eratosthenes - Finding Primes Python
I made a one liner version of the Sieve of Eratosthenessieve = lambda j: [print(x) for x in filter(lambda n: 0 not in map(lambda i: n % i, range(2, n)) and (n!=1)&(n!=0), range(j + 1))]In terms of...
View ArticleAnswer by Vlad Bezden for Sieve of Eratosthenes - Finding Primes Python
Using recursion and walrus operator:def prime_factors(n): for i in range(2, int(n ** 0.5) + 1): if (q_r := divmod(n, i))[1] == 0: return [i] + factor_list(q_r[0]) return [n]
View ArticleAnswer by Prokhozhii for Sieve of Eratosthenes - Finding Primes Python
Probably the quickest way to have primary numbers is the following:import sympylist(sympy.primerange(lower, upper+1))In case you don't need to store them, just use the code above without conversion to...
View ArticleAnswer 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...
View ArticleAnswer by lonny for Sieve of Eratosthenes - Finding Primes Python
not sure if my code is efficeient, anyone care to comment?from math import isqrtdef isPrime(n): if n >= 2: # cheating the 2, is 2 even prime? for i in range(3, int(n / 2 + 1),2): # dont waste time...
View ArticleAnswer by storm125 for Sieve of Eratosthenes - Finding Primes Python
Using a bit of numpy, I could find all primes below 100 million in a little over 2 seconds. There are two key features one should noteCut out multiples of i only for i up to root of nSetting multiples...
View ArticleAnswer by Pythoscorpion for Sieve of Eratosthenes - Finding Primes Python
i think this is shortest code for finding primes with eratosthenes methoddef prime(r): n = range(2,r) while len(n)>0: yield n[0] n = [x for x in n if x not in range(n[0],r,n[0])]print(list(prime(r)))
View ArticleAnswer by Madiyar for Sieve of Eratosthenes - Finding Primes Python
The fastest implementation I could come up with:isprime = [True]*Nisprime[0] = isprime[1] = Falsefor i in range(4, N, 2): isprime[i] = Falsefor i in range(3, N, 2): if isprime[i]: for j in range(i*i,...
View ArticleAnswer by nhern121 for Sieve of Eratosthenes - Finding Primes Python
import mathdef sieve(n): primes = [True]*n primes[0] = False primes[1] = False for i in range(2,int(math.sqrt(n))+1): j = i*i while j < n: primes[j] = False j = j+i return [x for x in range(n) if...
View ArticleAnswer by Tom Russell for Sieve of Eratosthenes - Finding Primes Python
I figured it must be possible to simply use the empty list as the terminating condition for the loop and came up with this:limit = 100ints = list(range(2, limit)) # Will end up emptywhile len(ints)...
View ArticleAnswer by FooBar167 for Sieve of Eratosthenes - Finding Primes Python
I prefer NumPy because of speed.import numpy as np# Find all prime numbers using Sieve of Eratosthenesdef get_primes1(n): m = int(np.sqrt(n)) is_prime = np.ones(n, dtype=bool) is_prime[:2] = False # 0...
View ArticleAnswer by Jules May for Sieve of Eratosthenes - Finding Primes Python
Here's a version that's a bit more memory-efficient (and: a proper sieve, not trial divisions). Basically, instead of keeping an array of all the numbers, and crossing out those that aren't prime, this...
View ArticleAnswer by Ajay for Sieve of Eratosthenes - Finding Primes Python
By combining contributions from many enthusiasts (including Glenn Maynard and MrHIDEn from above comments), I came up with following piece of code in python 2:def simpleSieve(sieveSize): #creating...
View ArticleAnswer by SilentDirge for Sieve of Eratosthenes - Finding Primes Python
My implementation:import mathn = 100marked = {}for i in range(2, int(math.sqrt(n))): if not marked.get(i): for x in range(i * i, n, i): marked[x] = Truefor i in range(2, n): if not marked.get(i): print i
View ArticleAnswer by MrHIDEn for Sieve of Eratosthenes - Finding Primes Python
Much faster:import timedef get_primes(n): m = n+1 #numbers = [True for i in range(m)] numbers = [True] * m #EDIT: faster for i in range(2, int(n**0.5 + 1)): if numbers[i]: for j in range(i*i, m, i):...
View ArticleAnswer by user3917838 for Sieve of Eratosthenes - Finding Primes Python
A simple speed hack: when you define the variable "primes," set the step to 2 to skip all even numbers automatically, and set the starting point to 1.Then you can further optimize by instead of for i...
View ArticleAnswer 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