Created
June 20, 2018 00:44
-
-
Save FrankHassanabad/b31a961f5907ee2ebe5b6af07e9bbfa0 to your computer and use it in GitHub Desktop.
Correction fo prime numbers
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
import math | |
# This as written will fail saying 1 and 4 are prime. | |
# Those two numbers are not prime. | |
# Also, checking up to square root is more efficient. | |
def IsPrime(n): | |
if (n == 1 or n == 4): | |
return False | |
for divisor in range(2, int(math.ceil(math.sqrt(n)))): | |
if n % divisor == 0: | |
return False | |
return True | |
# See table testing from earlier bathroom conversations | |
# where you would compare this to a table of known prime numbers | |
# This is ad-hoc for testing purposes only | |
for i in range(1, 100): | |
isPrime = IsPrime(i) | |
if isPrime: | |
print(i, IsPrime(i)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
As written this will falsely display 1 and 4 as prime numbers. See ad-hoc unit test below which when run displays these two as True