mirror of
https://github.com/TheAlgorithms/Python.git
synced 2025-07-05 09:21:13 +08:00
Add doctest to maths/sieve_of_eratosthenes.py and remove other/finding_primes.py (#1078)
Both of the two files implemented sieve of eratosthenes. However, there was a bug in other/finding_primes.py, and the time complexity was larger than the other. Therefore, remove other/finding_primes.py and add doctest tomaths/sieve_of_eratosthenes.py.
This commit is contained in:

committed by
Christian Clauss

parent
c27bd5144f
commit
46bc6738d7
@ -1,21 +0,0 @@
|
||||
'''
|
||||
-The sieve of Eratosthenes is an algorithm used to find prime numbers, less than or equal to a given value.
|
||||
-Illustration: https://upload.wikimedia.org/wikipedia/commons/b/b9/Sieve_of_Eratosthenes_animation.gif
|
||||
'''
|
||||
from __future__ import print_function
|
||||
|
||||
|
||||
from math import sqrt
|
||||
def SOE(n):
|
||||
check = round(sqrt(n)) #Need not check for multiples past the square root of n
|
||||
|
||||
sieve = [False if i <2 else True for i in range(n+1)] #Set every index to False except for index 0 and 1
|
||||
|
||||
for i in range(2, check):
|
||||
if(sieve[i] == True): #If i is a prime
|
||||
for j in range(i+i, n+1, i): #Step through the list in increments of i(the multiples of the prime)
|
||||
sieve[j] = False #Sets every multiple of i to False
|
||||
|
||||
for i in range(n+1):
|
||||
if(sieve[i] == True):
|
||||
print(i, end=" ")
|
Reference in New Issue
Block a user