from sympy import isprime
isprime(11)
Execution example
print(isprime(11))
print(isprime(12))
Execution result
True
False
However, if the argument is in float format (for some reason) it will always return False, so you need to convert it to an int.
Execution example
print(isprime(11))
print(isprime(11.0))
#Convert argument to int type
print(isprime(int(11.0)))
Execution result
True
False
True
import math
def isprime(x):
if x == 1:
return False
if x == 2:
return True
for i in range(2, int(x**0.5) + 1):
if x % i == 0:
return False
return True
Execution example
print(isprime(11))
print(isprime(12))
Execution result
True
False
I got the impression that sympy has various functions related to prime numbers. For example, how many prime numbers are included in the range of (a, b), a function to find the nth largest prime number, and so on. We plan to summarize it in the future. Click here for details (https://www.geeksforgeeks.org/prime-functions-python-sympy/)
Recommended Posts