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

hey bros I don’t like this I am pretty sure there is an additional step being made which is not necessary and shouldn’t be there.

def is_prime(x):
    if x < 2:
        return False
    else:
        for n in range(2, x-1): #I MEAN THIS LINE RIGHT HERE
            if x % n == 0:
                return False
        return True

print is_prime(13)
print is_prime(10)

Why is there the (-1) I don’t think it should be there. Or I am just tired and it still counts it from 0… like. Range(2)… 0, 1 and then stop at x-1 for instance 10 which would mean 9, numbers 2 to 9 that is… 1, 2, 3, 4, 5, 6, 7, 8, 9… oh god damnit I see now, somebody please confirm I understand. Wait no need to I get it.

2 Likes

3 posts were split to a new topic: Why doesn’t this work?

3 posts were split to a new topic: Why is n=2 returning true?

2 posts were split to a new topic: Why doesn’t -10 work?

None indicate the absence of a return value, thus no return keyword in your function is reached. Why do you expect False?

1 Like

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…