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

Why indeed. The only reason that comes to mind is a literal description of how the upper bound of the returned range object is x - 1. Trivial and unimportant.

The logical limit for this loop is the square root of x. After that it just mirrors itself.

for n in range(2, x ** 0.5 + 1):

Why do we use the second return false in this code? Don’t we either want it to return true or return false?

The first return of False is for any values less than 2, which is the smallest prime number by definition. 1 is not prime.

Within in the loop we are testing for divisibility. A prime is not divisible by anything but itself and 1. If we find a number to be divisible it is not prime so we return False immediately, rather than continue the loop.

1 Like

I checked this code on python interpretation, it is work, but on-site Codecademy not work, i have many errors, which is not true:

def is_prime(x):
    if x == 0 or x == 1:
        print "False"
        return False
    elif x == 2:
        print "True"
        return True
    n = x - 1
    while n > 1:
        if x % n == 0:
            print "False"
            return False
        if n == 2:
            print "True"
            return True


You don’t need to loop until NUMBER-1
to check prime number loop until NUMBER/2 it is work fine
you cannot find any factor of NUMBER beyond NUMBER/2
here my code

def is_prime(x) :
  print x
  if x < 2 :
    return False
  for n in range(2, x/2) :
    if x % n == 0:
      print "NOT PRIME"
      return False
  else :
    print "PRIME"
    return True

2 posts were merged into an existing topic: Why does is_prime fail for some numbers?

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


Instead of hardcoding is_prime(), how can I modify the code to ask for user input?

you could use input() (python3) or raw_input() (python2) and then cast to integer, but I would do this outside the codecademy environment

Why does x=2 return True if 2%2=0?

Because the if clause is never reached

The for loop has no iterations to make, the loop condition evaluates false from the get go

I am a bit confused by some of this exercise, I managed to figure out I needed to use a for loop with the range function, and I also needed to use the % with an if statement. Like quite a few people in here, I don’t understand what the purpose of x-1 has, I’m also confused on what n is meant to represent, and how does the programme function when n hasn’t actually been defined, for example in a simple function when introducing an argument variable, doesn’t it usually need to be defined by doing n = 1 or similar, I’m also confused on why after the second if statement it returns false I thought it should return true (this might make more sense when I understand what value n represents), below that it return true?

The author has meant for us to iterate up the last value of range(x) which is x-1 since the last value (x) is excluded.

range(10)  =>  [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

As for the n, well, if you have learned how a for loop works, you will know that n is the iteration variable that holds each value, in turn, as the loop progresses.

The % is the remainder operator. When one number is divided by another, if the remainder is zero then the number is evenly divisible by the other.

9 % 3  =>  0

which means that 3 divides evenly into 9 (there is no remainder). If a number can be divided evenly it cannot be a prime number since they are indivisible (save by itself or 1).

Today’s learners are so used to doing all their math work with a calculator or phone they either never learned or have forgotten how to use long division with pencil and paper.

Wouldn’t that mean that if x = 12, then wouldn’t x - 1 mean the range would go from 0 to 11 and not 12, but I thought the point was to go up to the value of x in this case 12, so would x + 1 not be the solution, as if the function stops at 11 then it would +1 and go up to 12?

So does this function check to see if the number is divisible between 1 and itself with no remainder, for example if the number is 3, does the function check:

3 % 3 = 0
3 % 2 = 0
3 % 1 = 0

Truth is we don’t need to cover the entire range, only the bottom half, since a number greater than half of n cannot be divided into n. In fact, all we really need to cover is all the numbers less than the square root of n plus 1. Furthermore, if all we have are primes in that range, we have enough to work with, but that’s not part of this lesson.

We would not do the first one since the range excludes 3. Nor would we do the last one since every number is divisible by 1. That leaves only the middle one, which will not equal zero, so we know that 3 is a prime.

If that is the case then does x-1 need to be included at all, could you not just use x?

Which is what the author meant. That one literal, x - 1 has wasted hundreds of peoples’ time all over nothing.

One last question, if for loops can’t iterate over an int, how can the for loop iterate over n, if n is represented by a range of int objects starting from 2 to the number before x?

The loop is not iterating over an integer, but over a range of integers. Each value is treated as a distinct number. A range is indexed, so can be iterated. In the example, n is only one value at a time. It is not n being iterated.

Here is my method, but shows this error in python 3, works fine in python 2

# Online Python compiler (interpreter) to run Python online. # Write Python 3 code in this online editor and run it. def is_prime(x): prime = True if x < 2: prime = False elif x == 2: prime = True else: for step in range(2, x): if x % step == 0: prime = False break if prime: print (" Is a prime number ") else: print (" Is not a prime number ") is_prime(int(input("Enter a number: "))) is_prime(int(input("Enter a number: ")))

what error do you get?