Skip to content

Instantly share code, notes, and snippets.

@beattidp
Last active February 22, 2021 00:39
Show Gist options
  • Save beattidp/2a1af09c69024028fd0e55d5e0e70561 to your computer and use it in GitHub Desktop.
Save beattidp/2a1af09c69024028fd0e55d5e0e70561 to your computer and use it in GitHub Desktop.
Calculate Prime Numbers in Python
''' calculate prime numbers '''
DEBUG = False
HIGH_NUMBER = 2000
# initial list of primes contains only the number 2.
known_primes = [ 2 ]
for n in range(known_primes[0]+1,HIGH_NUMBER,2):
if DEBUG:
print(" -------------- %d", n)
a = n
for p in known_primes:
# first prime which divides evenly disqualifies n as prime
if n % p == 0:
a = None
if DEBUG:
print('Note: %d is not prime (divisible by prime %d)' % (n, p))
break
if a is not None:
known_primes.append(a)
print(known_primes)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment