-
Notifications
You must be signed in to change notification settings - Fork 1k
/
Copy pathSieve-of-eratosthenes.py
45 lines (35 loc) · 980 Bytes
/
Sieve-of-eratosthenes.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
'''
The sieve of Eratosthenes is an algorithm for finding
all prime numbers up to any given limit. It is
computationally highly efficient algorithm.
'''
def sieve_of_eratosthenes(n):
sieve = [True for i in range(n + 1)]
p = 2
while p ** 2 <= n:
if sieve[p]:
i = p * p
while i <= n:
sieve[i] = False
i += p
p += 1
length = len(sieve)
for i in range(2, length):
if sieve[i]:
print(i, end=" ")
def main():
print("Enter the number upto which prime numbers are to be computed: ")
n = int(input())
print("The number of prime numbers less than " + str(n) + " is :")
sieve_of_eratosthenes(n)
if __name__ == "__main__":
main()
'''
Sample I/O:
Enter the number upto which prime numbers are to be computed:
30
The number of prime numbers less than 30 is :
2 3 5 7 11 13 17 19 23 29
Time complexity : O(n*(log(log(n))))
Space complexity : O(n)
'''