-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsieve_of_eratosthenes.py
More file actions
79 lines (59 loc) · 1.76 KB
/
sieve_of_eratosthenes.py
File metadata and controls
79 lines (59 loc) · 1.76 KB
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
"""
Sieve of Eratosthenes
The Sieve of Eratosthenes is an efficient algorithm to find all prime numbers
up to a given limit. It works by iteratively marking multiples of each prime
starting from 2.
Time Complexity: O(n log log n)
Space Complexity: O(n)
"""
def sieve_of_eratosthenes(n):
"""
Finds all prime numbers up to n using Sieve of Eratosthenes.
Args:
n: Upper limit (inclusive)
Returns:
List of prime numbers up to n
"""
if n < 2:
return []
# Create boolean array: is_prime[i] = True if i is prime
is_prime = [True] * (n + 1)
is_prime[0] = is_prime[1] = False
# Sieve: mark multiples of primes as composite
for i in range(2, int(n ** 0.5) + 1):
if is_prime[i]:
# Mark all multiples of i as composite
for j in range(i * i, n + 1, i):
is_prime[j] = False
# Collect all primes
primes = [i for i in range(2, n + 1) if is_prime[i]]
return primes
def is_prime(n):
"""
Checks if a number is prime using trial division.
More efficient for single number checks.
Args:
n: Number to check
Returns:
True if prime, False otherwise
"""
if n < 2:
return False
if n == 2:
return True
if n % 2 == 0:
return False
# Check odd divisors up to sqrt(n)
for i in range(3, int(n ** 0.5) + 1, 2):
if n % i == 0:
return False
return True
# Example usage
if __name__ == "__main__":
n = 50
print(f"Prime numbers up to {n}:")
primes = sieve_of_eratosthenes(n)
print(f" {primes}")
print(f" Count: {len(primes)}")
print(f"\nIs {17} prime? {is_prime(17)}")
print(f"Is {20} prime? {is_prime(20)}")