-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1676 from sprtkd/master
Added Sieve of Erathosthenes
- Loading branch information
Showing
2 changed files
with
29 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]) |