Is_prime Problem


#1

I'm really stumped that I got this wrong constantly. After consulting some Q&A places, I still can't understand the logic behind it. Help please.

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

This error comes out:

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

#2

point1.

It means if any number is greater or equal to 2 ,It should return True.
As this if-statement comes at top order and it returns the value, say if you pass 4, as its satisfying this condition, It just return True even its not a prime number.

return is final exit door of function,when function executes this, it just halt execution and gets of of the body ,even there are codes under it,It does not execute it.

so if you want to check for just 2 , then x == 2 will be enough.

point2.
also this one.

if x % n == 1:

if one number is divided by other number it should yield 0 , as even 11 is a prime number ,It will return False.

point3.
Also final else part inside the for loop.
suppose x is 9..
when you loop over it...
n is looping on range(2,9) which is a list... [2,3,4,5,6,7,8]
at first loop n takes value of 2 and as 9 is not divisible by 2, The test x % n == 0 turns out to be False and else code executes and it returns True but It had to return False

So you can do two things..
1.Either you should delete else clause and just return True or..

or

2.Use for/else Clause by indenting else clause as same level as for loop


#3

Hi nabeelkkb, your code below:

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

1st mistake:
if x >= 2:
You would be returning all numbers from 2 onwards as true. Then there's no point in doing the other steps anymore. Change this line to if x == 2: instead.
Hence, all numbers 2 and below will be dealt with in your first 2 statements. All numbers above 2 will be dealt with in our Loop later.

2nd mistake:
for n in range(2, x-1)
You are excluding x-1 from the range. Hence, correct way should be range(2, x)

3rd mistake:
if x % n == 1:
x%n gets the remainder of x/n.
If x gets divided perfectly by n, the remainder should be zero, NOT 1.
Hence, correct line should be if x % n == 0

Lastly,
Your last line of 'else' usage is incorrect.
You need to allow your 'for' loop to run and repeat through the whole range.
After you run the whole range and none of the n values caused x % n = 0, you will exit the loop and return True.
But if the for loop runs and a value of n caused x % n = 0, you will return False inside the loop.

If not, once the first n value fails the if condition, it will straight away return True without going through the whole range of n values.


#4

After making the adjustments, this will work.

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

#5

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