Last active
December 6, 2022 15:28
-
-
Save EteimZ/0d1c466d692feb79cd23f8e4ecbb9cf3 to your computer and use it in GitHub Desktop.
The Sieve of Erathosthenes is an algorithm used to calculate prime number to a certain range
This file contains hidden or 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
| # maximum range | |
| n = 100 | |
| # prime numbers | |
| primes = [] | |
| numbers = list(range(2, n+1)) | |
| while numbers: | |
| # Assume the first number is prime | |
| prime = numbers.pop(0) | |
| primes.append(prime) | |
| # remove all numbers divisible by the prime number | |
| numbers = [ x for x in numbers if x % prime != 0 ] | |
| print(primes) | |
| # [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97] | |
| """ | |
| The time complexity of the Sieve of Eratosthenes algorithm is O(n log log n). | |
| """ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment