Last active
February 22, 2021 00:39
-
-
Save beattidp/2a1af09c69024028fd0e55d5e0e70561 to your computer and use it in GitHub Desktop.
Calculate Prime Numbers in Python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' 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