Quantcast
Browsing all 28 articles
Browse latest View live

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 Article


Answer 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 Article


Answer 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 Article

Answer 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 Article

Answer 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 Article


Answer 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 Article

Answer 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 Article

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...

View Article


Answer 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 Article


Answer 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 Article

Answer 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 Article

Answer 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 Article

Answer 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 Article


Answer 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 Article

Answer 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 Article


Answer 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 Article

Answer 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 Article


Answer 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 Article

Answer 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 Article

Answer 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 Article
Browsing all 28 articles
Browse latest View live