Fails on is_prime(2)


#1

I am not sure what is wrong with my code, it makes sense in my head:

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

Is_prime: Working Fine. But not for 3 as a Prime Number
#2

The rules for finding primes are as follows,

  1. Anything negative or 1/0 is not a prime
  2. Any number divisible by only its' self or one is a prime

Alrighty then, let's program that.

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

Easy enough?

Remember when return is called it immediately breaks out of the function with that value.

x = 0
y = 2
Also range works like this, starts with x, ends with y-1.


#3

Thank you!
How come when I had an else: statement inside the for loop it would fail but when I took it out and wrote the code as you suggested it worked?

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

This would fail when they used the number is_prime(2)

how is that ^ different than this:

for n in range(2, x):
    if x % n == 0:
        return False
return True

#4

You leave it like that but remember if it does not pass the if statement of ether of those conditions it still has to return True.

So doing it like so

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

This would also work, if you eliminate all other possibilities it still hs to return True. That is why it would not work is because it would return None when it needed to return True.


#5

Hi Zeziba, was wondering if you mind explaining this line by line. Thank you so much.


#6

Actually after a little research I have redone my is_prime function.I needed something faster for a task I was doing with huge numbers.

def is_prime(number):
    """
    This function computes all the whole numbers from 3 to the square root of the
    number + 1, this takes out a bunch of un-need computations.
    :param number: Number to be computed
    :return: Returns True if is prime Else False
    """
    if number > 1:
        # This line checks if the number is greater than one, if not returns False
        if number == 2:
            # This line checks to see if the number is 2, if so Returns True
            return True
        if number % 2 == 0:
            # This line checks to see of the number is divisible by two if so Returns False
            return False
        for current in range(3, int(sqrt(number) + 1), 2):
            # This line iterates through the range of numbers from 3 to the square root of
            # the number + 1 with a stride of two
            # So numbers would be non-even numbers
            if number % current == 0:
                # If a number divides into our number Returns False
                return False
        return True
        # Returns True if non of the previous tests failed
    return False

EDIT:

You also need this.

from math import sqrt

#7

I tried this but it returned an error about the global_name.
Also, can you explain the use of int(sqrt(number) + 1)? :x


#8

You have to import the math modules sqrt function.

from math import sqrt

#9

You get the sqrt of the number so you don't try to figure out if it is divisible by every number in it's range.

Here is a link that explains the math behind why I am doing this. This math orks on primes under 10,000,000,000 easily.