Skip to content

Instantly share code, notes, and snippets.

@mkpoli
Created April 17, 2022 14:41
Show Gist options
  • Select an option

  • Save mkpoli/4811f795270cdbb66bb74c8b1771cb43 to your computer and use it in GitHub Desktop.

Select an option

Save mkpoli/4811f795270cdbb66bb74c8b1771cb43 to your computer and use it in GitHub Desktop.
A Simple Implementation of Sieve of Eratosthenes (with Checker)
from collections import defaultdict
from venv import main
import sympy as sp
def print_table(table: dict[int, bool], number: int):
for i in range(1, number + 1):
print(f'{i:2}: {"T" if table[i] else "F"}', end='; ')
print()
print()
def check_result(table: dict[int, bool], number: int):
false_positives = []
false_negatives = []
for i in range(1, number + 1):
if sp.isprime(i) and table[i] is False:
false_negatives.append(i)
elif not sp.isprime(i) and table[i] is True:
false_positives.append(i)
print('Result:')
print(f' False positives ({len(false_positives)}): {false_positives}')
print(f' False negatives ({len(false_negatives)}): {false_negatives}')
def main():
number = int(input('Enter a number: '))
for i in range(1, number + 1):
print(i)
table = defaultdict(lambda: True)
table[1] = False
for i in range(2, number + 1):
if table[i]:
for i in range(i * 2, number + 1, i):
table[i] = False
print_table(table, number)
check_result(table, number)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment