Is_prime help?


#1



https://www.codecademy.com/courses/python-intermediate-en-rCQKw/0/6?curriculum_id=4f89dab3d788890003000096#


It says it fails when using number 3; however it works just fine when using a python console on my local machine?


the same result as using the modulus that is in other threads


def is_prime(x):
    prime = True
    if x < 2:
        prime = False
    elif x == 2:
        prime = True
    else:
        for num in range(2, x):
            value = x / num
            if value - int(value) == 0:
                prime = False
                break
    return prime


#2

It works in Python 3, but not Python 2.

x / num

will be an integer.

float(x) / num

will be a float, whereby your method works fine.

Aside

Because of the extra logic, this method may prove to be slow on larger numbers.

is_prime(997)

showed some lag in the lab. A number that small should be instantaneous.


#3

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