Created
May 4, 2020 02:50
-
-
Save karimlahlou/76ff79f660e4cf7dc8da6afd837a4a50 to your computer and use it in GitHub Desktop.
Check if the number is a prime and digits ending with 0,2,4,5,6,8 are not primes!
This file contains hidden or 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
def is_prime(num): | |
if num <= 1: | |
return False | |
if num <= 3 or num < 9 and num % 2 != 0: | |
return True | |
# If the last digit contain one of the following | |
# or if the number is divisible by 2 or 3 | |
if int(str(num)[-1]) not in [1, 3, 7, 9] or num % 2 == 0 or num % 3 == 0: | |
return False | |
i = 5 | |
while(i * i <= num) : | |
# If the number is divisible by i or i + 2, it is not prime | |
if (num % i == 0 or num % (i + 2) == 0) : | |
return False | |
i += 6 | |
return True | |
for i in range(0, 100): | |
if is_prime(i): | |
print(i) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment