I have a code to know whether a number is prime or not ,but the problem is I want the output a boolean value i.e.,True or False

Submitted 3 years, 7 months ago
Ticket #175
Views 239
Language/Framework Python
Priority Medium
Status Closed

def isPrime(x):
    if x > 1:
        for i in range(2, x):
            if x % i == 0:
                return False

        return True

    return False

print(isPrime(int(input("Enter a number: "))))

Though it returns the same output as I desired but it is not the efficient code........

So can anyone provide me the efficient code...............

Submitted on Sep 21, 20
add a comment

1 Answer

Verified

def isPrime(x):
    x = int(x)
    for i in range(2, x):
        if x % i == 0:
            return False
    return x >= 2
                
print(isPrime(input("Enter a prime number.")))

Then the output will be

OUTPUT:

Enter a prime number.89
True

Submitted 3 years, 6 months ago


Latest Blogs