Prime Check Function

Prime Check Function

Easy Python Functions 23 views
Explanation Complexity

Problem Statement

Read one integer n. Write function is_prime(n) and output YES if prime else NO.

Input Format

One integer n.

Output Format

YES or NO.

Example

29
YES

Constraints

0

Input / Output Format

Input Format
One integer n.
Output Format
YES or NO.
Constraints
0

Examples

Input:
29
Output:
YES

Example Solution (Public)

Python
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')

Official Solution Code

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')
Please login to submit solutions.
Editor
Output

                                        
Please login to submit solutions.