Skip to content

Commit

Permalink
Update math.py
Browse files Browse the repository at this point in the history
Solve bug in function "prime_sieve"
  • Loading branch information
gcgeng authored Nov 8, 2017
1 parent 656af22 commit 481db67
Showing 1 changed file with 6 additions and 5 deletions.
11 changes: 6 additions & 5 deletions cyaron/math.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,19 +226,20 @@ def catalan_number(n):
def prime_sieve(n):
"""
Return a list of prime numbers from 2 to a prime < n. Very fast (n<10,000,000) in 0.4 sec.
Example:
>>>prime_sieve(25)
[2, 3, 5, 7, 11, 13, 17, 19, 23]
Algorithm & Python source: Robert William Hanks
http://stackoverflow.com/questions/17773352/python-sieve-prime-numbers
"""
sieve = [True] * int(n/2)
sieve = [True] * (n//2)
for i in range(3,int(n**0.5)+1,2):
if sieve[int(i/2)]:
sieve[i*int(i/2)::i] = [False] * (int((n-i*i-1)/(2*i))+1)
return [2] + [2*i+1 for i in range(1,int(n/2)) if sieve[i]]
if sieve[i//2]:
sieve[i*i//2::i] = [False] * ((n-i*i-1)//(2*i)+1)
return [2] + [2*i+1 for i in range(1,n//2) if sieve[i]]



#--- bezout coefficients--------------------------------------------------------------------------
Expand Down

0 comments on commit 481db67

Please sign in to comment.