6. is_prime: Why % versus /?


#1

So I was looking at the code here and I came across a few questions:

def is_prime(x):
    if x < 2:
        return False
    else:
        for n in xrange(2, x):
            if x%n == 0:
                return False
        return True

Why can't we use this code?

   def is_prime(x):
        if x < 2:
            return False
        else:
            for n in xrange(2, x):
                if x / n != 1:
                    return False
            return True

I'm trying to figure out why that isn't okay through the program.

Thanks in advance!


#2

Division gives a quotient. The remainder operator gives, well, a remainder. Math is anything but simple, but fortunately this one is not outside our grasp.