Prime Check Function
Python
Easy
2 views
Problem Description
Read one integer n. Write function is_prime(n) and output YES if prime else NO.
Input Format
One integer n.
Official Solution
import sys,math
s=sys.stdin.read().strip()
if not s: sys.exit(0)
n=int(s)
def is_prime(x):
if x<2:
return False
if x%2==0:
return x==2
r=int(math.isqrt(x))
d=3
while d<=r:
if x%d==0:
return False
d+=2
return True
sys.stdout.write('YES' if is_prime(n) else 'NO')
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!