Skip to content

Instantly share code, notes, and snippets.

@karimlahlou
Created May 4, 2020 02:50
Show Gist options
  • Save karimlahlou/76ff79f660e4cf7dc8da6afd837a4a50 to your computer and use it in GitHub Desktop.
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!
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