Is_prime


#1

def is_prime(x):
    n = 0
    n = x * (x-1)
    if x % n == 0:
        return False
    else:
        return True

I am struck at this level and not able to understand what to do. Can someone explain me in detail.


#2

woah woah, you over complicated it dude, lets go back to highschool maths, ok?
so what is the property that defines a prime? it can only be divisible by itself and one,
so we want to test if our number (x) doesnt divide into anything besides itself, and one.

def is_prime(x):
      for i in range(2,x): #iterates for every number smaller than x and bigger or equal to 2
         if x % i == 0:  #Checks if that number is divisible by our number
             return false
      return true #once its done, and it hast found anything that is divisble by x, it returns true

#3

Oops, try again. Your function fails on is_prime(0). It returns True when it should return False.


#4

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.