Skip to content

Commit

Permalink
Merge pull request #1676 from sprtkd/master
Browse files Browse the repository at this point in the history
Added Sieve of Erathosthenes
  • Loading branch information
ZoranPandovski committed Oct 21, 2018
2 parents c465745 + b2267f7 commit 9502fce
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
1 change: 1 addition & 0 deletions IMPLEMENTATIONS.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
* [Math](math)
* [russian peasant](math/russian_peasant)
* [towers of hanoi](math/towers_of_hanoi)
* [Sieve of Eratosthenes](math/sieve%20of%20eratosthenes)
* [armstrong number](math/armstrong_number)
* [euclid's gcd](math/euclids_gcd)
* [prime seive](math/prime_seive)
Expand Down
28 changes: 28 additions & 0 deletions math/sieve of eratosthenes/python/sieveOfEratosthenes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
def SieveOfEratosthenes(n):
'''
This function returns a list
which can be used for lookup of a number
to check whether it is prime or not
Input: max number to be looked up
'''
prime = [True for i in range(n+1)]
p = 2
while (p * p <= n):
if (prime[p] == True):
for i in range(p * 2, n+1, p):
prime[i] = False
p += 1

prime[0]=False
prime[1]=False
return prime


if __name__ == "__main__" :
'''example use'''
max_val = 10
lookup_list = SieveOfEratosthenes(max_val)
for num in range(max_val+1):
print(num,': ', lookup_list[num])

0 comments on commit 9502fce

Please sign in to comment.