Prime or Not
Python
Medium
2 views
Problem Description
Read integer n, output PRIME if it is prime else NOT. Use loop up to sqrt and break early.
Input Format
One integer n.
Output Format
PRIME or NOT.
Official Solution
import sys
s=sys.stdin.read().strip()
if not s: sys.exit(0)
n=int(s)
if n<2:
sys.stdout.write('NOT')
else:
i=2
ok=True
while i*i<=n:
if n%i==0:
ok=False
break
i+=1
sys.stdout.write('PRIME' if ok else 'NOT')
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!