Practice make Perfect : is_prime Help!


This is all i got...and i don't know what to do from here on. Can someone give me some hints on what to do next?

Replace this line with your code. 
def is_prime(x):
    if (x > 2):
        return False


You need a for loop that checks to see if i is in the range made by the div and x. Inside that, you need to check if x modulo i is equal to 0, and return the appropriate response.


what is "div" if i do for i in range()?


div is the variable name I used to define the start of my range, which I decided should be the first prime number, 2. The full code is:

def is_prime(x):
    if (x > 1):
        div = 2
        for i in range(div,x):
            if (x % i) == 0:
                return False
        return False
    return True

If you examine the code, you should be able to work out the logic


The range has nothing to do with prime numbers. We start with 2 so that all even values for x are elliminated right off the top. No point continuing the loop if the number is even.

There is no real point in assigning 2 to a variable. It creates confusion for the reader.

for i in range(2, x):

tells the reader what is going on.

Parens are not required in either instance, and it is probably better to not use parens unless they are needed for grouping.

if x > 1:

if x % i == 0:


Haha, im so confused now


Think about what a Prime number is...

  1. A positive integer greater than 1
  2. Not divisible by any number other than itself and 1
  3. Other than 2, all are odd numbers.

We are using a brute force method to test primeness which is commonly known as the divisibility approach. Any number that can be evenly divided by a number less than itself is not a prime.

The first step we take will be to elliminate any inputs less than 2.

if x < 2: return False

# loop and divisibilty test goes here

return True


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

error: Oops, try again. Your function fails on is_prime(2). It returns None when it should return True.

BTW the reason why i am having problem with this is because i truly still dont understand prime numbers.


This should be return False since it is divisible. Leave the return True line where I wrote it in the example above.


thanks for helping...
Would you mind explain me the range(2,x) and why do i have to add return True to the last line.

For example:
lets x be 4
for i in range( 2 , 4): <---------- what exactly is range(2,4)?
if x % i = 0 <---------- what is i?


You don't have to add it, just leave where it is.

if x < 2: return False

# loop and divisibilty test goes here

return True

Why? Because 2 does not enter the loop so will drop to this line. Otherwise, only the values for x that survive the divisibility test will reach this line.


I know I'm dumb, but I still don't get it. :slight_smile: Here is the code:

def is_prime(x):
    if (x > 1):
        for i in range(2,x):
            if (x % i) == 0:
                return False
        return False
    return True

If we go step by step:
The function starts.
If x is greater than 1, so if it is 2 or 3 or 4 etc, the loop starts and checks for prime. But why isn't there an else condition for the if x % i == 0 statement?
Then, if x is smaller than 1, meaning 0, it returns False. That I understand.
And then the last line is not connected to any if statements, so shouldn't it always returns True when the functions runs?


I might be wrong but the way I see it:
The problem requires the range to be starting from 2 so if x < 2 it should return false.. the second statement refers to the range we're looking for and reads that if x within that range is divisible by n then we return False.. the final statement refers to all other cases not previously covered and those return the opposite (in this case True)..

You don't need the return False statement after else


Remove those two lines, they are not needed.


So what your say is that in range(2,x) that python will drop any number that is even and if the number is the odd it will go through the divisible by test, correct?


Actually, all even numbers go through the test, but fail immediately. Odd numbers will take at least two iterations of the loop to fail. If the loop completes without failing, the number is prime so the last line returns True.


What makes it to fail? I am still not understanding what range(2,x) <--- why is there a 2 and what does it do with the function range()?


x = 9
for n in range(2, x):
    print n,

# 2 3 4 5 6 7 8

If x is even, it will return False when n is 2. The input 9 will fail when n is 3.

x = 19
for n in range(2, x):
    print n,

# 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18

When x is 19 the loop will iterate the complete range without returning False, so the function will return True. 19 is prime.


x = 21
for n in range(2, x):
    print n,

# 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20

When x is 21, it will fail when n is 3.


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