My backwards logic

Ian Kelly ian.g.kelly at gmail.com
Fri Sep 5 23:04:03 CEST 2014


On Fri, Sep 5, 2014 at 11:44 AM, Seymore4Head
<Seymore4Head at hotmail.invalid> wrote:
> BTW since I am getting no grade, I much prefer the answer than a hint.
> The best hint IMO is to tell me how you would do it.

from math import ceil, sqrt

def is_prime(n):
    if n < 2:
        return False
    if n % 2 == 0:
        return n == 2
    return all(n % x != 0 for x in range(3, int(sqrt(n)) + 1, 2))

while True:
    n = int(input("Enter a number: "))
    if is_prime(n):
        print("{} is prime.".format(n))
    else:
        print("{} is not prime.".format(n))



More information about the Python-list mailing list