Skip to content

Instantly share code, notes, and snippets.

@richardiwnl
Created July 27, 2022 22:30
Show Gist options
  • Save richardiwnl/e3b6765ca0bafe94cff7993426c8ee62 to your computer and use it in GitHub Desktop.
Save richardiwnl/e3b6765ca0bafe94cff7993426c8ee62 to your computer and use it in GitHub Desktop.
Looks for prime numbers until the nth element
import argparse
parser = argparse.ArgumentParser(
description='Calculates prime numbers until the nth element',
prog='primes',
epilog='Made with love by github.com/richardcss'
)
parser.add_argument(
'n',
action='store',
help='The limit to look for prime numbers',
metavar='n',
type=int,
)
args = parser.parse_args()
UP = '\x1B[4A'
CLR = '\x1B[K'
def is_prime(n: int) -> bool:
if n in (0, 1):
return False
divisions = 0
for i in range(2, n // 2 + 1):
if n % i == 0:
divisions += 1
return divisions < 1
primes = 0
for i in range(args.n + 1):
print(f'{UP}Iteration count: {i}{CLR}\nPrime numbers count: {primes}{CLR}\nProgress: {i / args.n * 100:.1f}%{CLR}\n')
if is_prime(i):
primes += 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment