FAQ: Learn Python - Practice Makes Perfect - factorial FAQ: Learn Python - Practice Makes Perfect - is_prime

2 posts were split to a new topic: Why does this fail on x=2?

Hi, why does is_prime(15) return True in the solution code, while 15 is not a prime number?

Can’t replicate issue, using the solution code:

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

print is_prime(13)
print is_prime(10)
print is_prime(15)

i get false for 15, as expected

what is false about my code:

def is_prime(x):
for e in x:
while e==range(2,x-1):
if e % n==0:
return False
else:
return True
print is_prime(17)
print is_prime(4)
print is_prime(1)

If x is a number, it is not iterable. Consider, do we need nested loops to solve this problem?

6 posts were split to a new topic: How to utilize a loop of for and else?

3 posts were split to a new topic: Do I need break or return?

2 posts were split to a new topic: Not able to understand the following program

def is_prime(x):
if x == 2:
return True
if x < 2:
return False
else:
j = 0
for n in range (2, x):
if x % n == 0:
return False
return True
print is_prime(2)
print is_prime(3)
print is_prime(4)
print is_prime(5)
print is_prime(6)
print is_prime(7)
print is_prime(8)
print is_prime(9)

Hi, I was wondering if someone could help me solve my code’s problem with the 9 (the print statements are only to verify answers as you can see). It doesn’t count 9 as a prime and I was wondering why. lately, I have been troubled by multiples and 9 appears to persist somehow.

Not sure what to make of this statement. Do we even need an else clause?

Sorry, the J = 0 was a line that I used from another idea to try to get the 9 as non-prime.

But thanks for pointing out the else, it was really useless and made me note another thing which allowed me to realize the error of the code

(I am not gonna tell the error as I feel I shouldn’t spoil the answer).

btw if a branch returns then the code that comes after the if-statement is already exclusive and doesn’t need to be inside else and the additional indentation that comes with it

if something
   exit like so
otherwise do something else

another thought is that the iteration you’re doing is “any” - if any of these numbers divide this number
several loop concepts have function equivalents, and any is one of them

And yeah I’m already using some silly fancy things.

return not any(x % d == 0 for d in range(2, x))

could also…

divisors = range(2, x)
dividesX = lambda d: x % d == 0
return not any(map(dividesX, divisors))

… I’m probably just making things complicated.
but at the same time, simpler, because I’m no longer writing the loop.

Figure out all the potential divisors (range)
Define a test for a single divisor (given a number d, does d divide x?)
Apply the test to each divisor (map)
Check if any passed (any)

1 Like

True. I believe this was one of the many mistakes I see people doing in this exercise (And I committed it myself as well), as you tend to think that you need to specify the program to go over other characteristics, despite it not being needed. (In reality, it wouldn’t make a difference, but rather add a line. Though, the fewer lines the better so…)

Not exactly. I mean, your second theory would be probably equivalent to the for loop idea in terms of lines, maybe faster as it is done through functions. But the first one seems pretty simple an good-looking.

If you range up to x, you will get modulo equals 0 even for prime numbers

Not a bug, but kind of misleading instruction:

This suggests that x is bigger than 2 while during test run you check for x=0 and x=1…

no you will not. you would need to include x for that to happen.

no it suggests that n should not exceed that amount, it says nothing about x’s value

Apologies for range pin, someone suggested range (2, x-1) and I forgot that range returns numbers except the last one.
As for " n from 2 to x - 1 ," with x=0 we have to count backwards (n=2, n=1, n=0, n=-1) and then forwards again :wink:
Also definition says " A prime number (or prime ) is a natural number greater than 1…" so I don’t understand why my code was tested for zero and one?

0 and 1 are valid inputs for your function. Your function is supposed to tell primes and non-primes apart, therefore both primes and non-primes are valid input.
If only primes were used as input then you could write your function like this:

def is_prime(n):
    return True

if you start at 0 and count upwards stopping before -5, then you will end up with zero values. This does not mean that -5 is greater than 0.

why can not this program run ? Why is my second else in white ?捕获

getting an error Your function fails on is_prime(0). It returns None when it should return False.!