mercredi 15 juin 2016

Function to check if number x is prime in python


What I've done so far is:

def check_prime(x):
  if x >= 2:
    for n in range(2, x - 1):
      if x % n == 0:
        return False
      else:
        return True
  else:
    return False

However when I check if any number >= 2 is a prime it returns None instead of True or False. check_prime(0) returns False and check_prime(1) returns False. Why does any number >= 2 return None and how can I fix this. 


Aucun commentaire:

Enregistrer un commentaire