is_prime: Inconsistent results between various platforms


#1

Hi Guys! I've been tackling these problems over the last little while. I got stuck on the is_prime problem but after a lot of thought and manual step through troubleshooting, I came up with a solution! It's not the most efficient and I know it can be improved, but I'm excited about having solved it without any external help =) Unfortunately, the solution seems to work fine on ideone.com but Code Academy gives me different results. Here is the code:

results = []

def is_prime(x):
    if (x <= 1):
        return False;
    if (x == 2 or x == 3):
        return True;
    for n in range (2, (x-1)):
        results.append(x % n)
    if 0 in results:
    	return False
    else:
    	return True

So, when I run this in Ideone with is_prime(5), I'll get True with the resulting list printout. But for whatever reason, the same code copy/pasted into Code Academy gives me "Oops, try again. Your function fails on is_prime(5). It returns False when it should return True." I've stepped through manually with x = 5; I can't see what's wrong here. Please help?


#2

@chippro79789

I would first ensure that your browser has no cached data as this could lead to a false positive.

Secondly, and try to get into the habit of this. Functions are meant to be total encapsulation of the function you are programming, meaning that it should be independent of all exterior code. So add the results list to your function it's self.

Secondly let's test your function on a larger list and compare it to a proven is_prime function. If they return the same results then your function is a go,

Your Code:

def is_prime(x):
    results = [] # Moved results here
    if (x <= 1):
        return False # ;
    if (x == 2 or x == 3):
        return True # ;
    for n in range(2, (x-1)):
        results.append(x % n)
    if 0 in results:
        return False
    else:
        return True

Proven Code:

def is_prime(number):
    from math import sqrt
    lowest_primes = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37,
                     41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101]
    if number > 1:
        if number in lowest_primes:
            return True
        for num in lowest_primes:
            if number % num == 0:
                return False
        for num in range(101, int(sqrt(number)) - 1, 2):
            if number % num == 0:
                return False
        return True
    return False

Ok, time to run the tests,

Tests:

def test():
    result_1 = [o_is_prime(number) for number in range(10000)]
    result_2 = [is_prime(number) for number in range(10000)]
    difference = 0
    for index, item in enumerate(result_1):
        if result_2[index] != item:
            difference += 1
    return difference

if __name__ == '__main__':
    print(test())

The grand total tally is a bit whopping 0 differences all the way up to 10,000. So there is a good chance your function is correctly written.

Like I said make sure there is not a lingering session still in memory and you will have no issues, also delete the commented out ; in your code. Just look at the example I have of your code.