Is_prime


#1

Could someone, help me to find the error? But, please don’t tell me the solution, only some tips.

Define a function called is_prime that takes a number x as input.
For each number n from 2 to x - 1, test if x is evenly divisible by n.
If it is, return False. If none of them are, then return True.

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

#2

No printing needed.

This will give false positives for any odd number.


#3

Thanks for answering.

I only use the print function to check whats happening with x and n. I know it’s not necessary, Thanks anyway.

I understood I am including 2 when I check if the x is even, so I changed the code but it’s not working yet.

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

#4

You return during each iteration of your loop, so there will only be one iteration. That isn’t a loop at all.


#5

As alluded to earlier, this is arbitrarily returning on the first iteration when x is odd. This loop should have a conditional return of False, only. No else clause is needed.

As for printing, that is moot since we know what number is passed in, and we can predict the iteration range. I think we can remove those lines by this point.

Here is your code, with the lines mentioned above all removed…

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

This is not solution code, just your own code reduced. There are still questions.


#6

Thanks for the tips. I removed the unnecessary lines, but I am still stuck here.

I am getting this error message even defining True when the number is 2:

Your function fails on is_prime(2). It returns False when it should return True.


#7

Refer to how you determine whether a number is prime manually. Read your code and compare to how you would do it yourself and consider what should be different. And if you don’t know what something does then you’ve found something you need to investigate before doing anything else.
Additionally you can add prints where you print out what is being done so that you can read the output to determine if it’s doing the right things (right order, right amount of times, starting with the right number, ending with the right number etc)


#8

What happens inside the loop when x is 2? This is one of the questions I said would still come up.


#9

Ok. I believe I understood that my code was not correct. I changed:

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

Now I am getting this error:

Your function fails on is_prime(5). It returns False when it should return True.

Am I in the right direction?


#10

Good, you have moved the test for 2 outside of the loop. As you may have by now discovered, when x is 2, the loop does not run.

That will not give the results we want. This method should be using divisibility as a test, meaning use the modulo operator.

if x % n == 0:

To repeat, this conditional should return False, only, and have no else clause.


#11

Nice.

I was trying to use module before but as I did not get the result I want I decided to change this comparison to a division.

Now it is working okay. Thanks for all the support.

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

#12

Using the above construct, three things can be concluded…

  1. The range need not be any greater than half of x.

  2. 2 does not enter the loop so will fall down to the last line if there is no check for twoness.

  3. When an if clause returns something, we do not need an else.

    if x < 2: return False
    for ...:
      if ...: return False
    return True

#13

very enlightening

Thanks.


#14

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