0

Can someone explain to me how this function works? I don't get how the for loop keeps going while there is return False after the if statement, which is also unclear to me.

def IsPrime(n):
    for x in range(2, int(n/2+1)):
        if not n % x:
            return False;
    return True

I don't understand what is happening in line 3 of this code.

4
  • 2
    It checks the truthiness of the expression. Commented Feb 8, 2018 at 22:28
  • 3
    Truth Value testing Commented Feb 8, 2018 at 22:31
  • 1
    n % x returns a number. Try bool(6) and bool(0) in the Python shell Commented Feb 8, 2018 at 22:36
  • Please indent your code properly. Commented Feb 8, 2018 at 23:01

3 Answers 3

1

In short: the if fires when n is dividable by x.

Background:

If you write something with:

if <expr>:
    pass

The not keyword also evaluates the truthiness, and in case the truthiness is True of the expression, then not expression is False and vice versa.

Python checks the truthiness of the <expr>. The truthiness is a boolean value that is associated with objects.

True and False have as truthiness respectively True and False, and None has truthiness False (so we can check if someobject usually to do an implicit None check).

Collections like lists, sets, dicts, tuples, etc. usually have truthiness True if and only if these collections contain at least one element. So empty collections have truthiness False. What these collections contain is of no importance for the truthiness of the collection itself.

Then there are also numerical types. Usually a number has truthiness False if and only if it is equal to zero, so negative and strictly positive numbers have truthiness True.

Generic objects have by default truthiness True, but you can override the __bool__ magic function to return a different truthiness, or if you override __len__ (and not __bool__), it will check if __len__ is greater than zero.

So if n and x are integers, then we calculate n % x, this thus performs a modulo check, and n % x is zero if and only if n is dividable by x.

Now the not operator will thus evaluate the truthiness. In case n is dividable by x, then not n % x is True, otherwise not n % x is False.

So the if is fired if n is dividable by x. The prime test thus checks if n is dividable by all numbers between 2 and n/2+1, and if not, it returns True, from the moment one is dividable, it returns False.

We can however speed up the calculations by iterating up to the square root of n, and make hops of two:

from math import sqrt

def IsPrime(n):
    if not n % 2:
        return False
    for x in range(3, int(sqrt(n)), 2):
        if not n % x:
            return False
    return True
Sign up to request clarification or add additional context in comments.

Comments

0

What follows after if, will be casted to type bool, i.e. even though you put an integer in, python will have to cast it to bool, because if can only branch on the boolean values True or False.
Now python evaluates all numbers except 0 to True in the bool(number) function. So basically if not n%x is the same thing as if not ((n%x) != 0).

Example x=2: code inside the loop will be executed for even values of n.

Comments

0

In line 3, you divide n by integers between 2 to n/2,now lets look at if not n%x:,here if x divides n completely then n%x returns 0 which is interpreted as False.Now not of False is True therefore condition evaluates to True and your IsPrime(n) function returns False.So,any number n which has a factor between 2 and n-1 or more precisely between 2 and n/2 is Not a Prime Number,So your function returned false else your function will evaluate to True.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.