Skip to content

Instantly share code, notes, and snippets.

@EteimZ
Last active December 6, 2022 15:28
Show Gist options
  • Select an option

  • Save EteimZ/0d1c466d692feb79cd23f8e4ecbb9cf3 to your computer and use it in GitHub Desktop.

Select an option

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
# 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