Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 48 additions & 34 deletions maths/prime_sieve_eratosthenes.py
Original file line number Diff line number Diff line change
@@ -1,49 +1,63 @@
"""
Sieve of Eratosthenes
# Sieve of Eratosthenes

Input: n = 10
Output: 2 3 5 7
# Input: n = 10
# Output: 2 3 5 7

Input: n = 20
Output: 2 3 5 7 11 13 17 19
# Input: n = 20
# Output: 2 3 5 7 11 13 17 19

you can read in detail about this at
https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes
"""
# you can read in detail about this at
# https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes
# """


# def prime_sieve_eratosthenes(num: int) -> list[int]:
# """
# Print the prime numbers up to n

# >>> prime_sieve_eratosthenes(10)
# [2, 3, 5, 7]
# >>> prime_sieve_eratosthenes(20)
# [2, 3, 5, 7, 11, 13, 17, 19]
# >>> prime_sieve_eratosthenes(2)
# [2]
# >>> prime_sieve_eratosthenes(1)
# []
# >>> prime_sieve_eratosthenes(-1)
# Traceback (most recent call last):
# ...
# ValueError: Input must be a positive integer
# """

def prime_sieve_eratosthenes(num: int) -> list[int]:
"""
Print the prime numbers up to n
# if num <= 0:
# raise ValueError("Input must be a positive integer")

>>> prime_sieve_eratosthenes(10)
[2, 3, 5, 7]
>>> prime_sieve_eratosthenes(20)
[2, 3, 5, 7, 11, 13, 17, 19]
>>> prime_sieve_eratosthenes(2)
[2]
>>> prime_sieve_eratosthenes(1)
[]
>>> prime_sieve_eratosthenes(-1)
Traceback (most recent call last):
...
ValueError: Input must be a positive integer
"""
# primes = [True] * (num + 1)

if num <= 0:
raise ValueError("Input must be a positive integer")
# p = 2
# while p * p <= num:
# if primes[p]:
# for i in range(p * p, num + 1, p):
# primes[i] = False
# p += 1

primes = [True] * (num + 1)
# return [prime for prime in range(2, num + 1) if primes[prime]]

p = 2
while p * p <= num:
if primes[p]:
for i in range(p * p, num + 1, p):
primes[i] = False
p += 1
def sieve(n):
isprime[0] = isprime[1] = False
for i in range(2, math.sqrt(n) + 1):

Check failure on line 49 in maths/prime_sieve_eratosthenes.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (F821)

maths/prime_sieve_eratosthenes.py:49:23: F821 Undefined name `math`
if isprime[i]:
primes.append(i)
for j in range(i*i, n+1, i):
isprime[j] = False

return [prime for prime in range(2, num + 1) if primes[prime]]
n = int(input())
isprime = [True for i in range(n+1)]
primes = []

print('The prime numbers from 1 to', n, 'are:')
print(*primes)

if __name__ == "__main__":
import doctest
Expand All @@ -51,4 +65,4 @@
doctest.testmod()

user_num = int(input("Enter a positive integer: ").strip())
print(prime_sieve_eratosthenes(user_num))

Check failure on line 68 in maths/prime_sieve_eratosthenes.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (F821)

maths/prime_sieve_eratosthenes.py:68:11: F821 Undefined name `prime_sieve_eratosthenes`
Loading