Is prime. A prime number is a natural number greater than 1 that has no positive divisors other than 1 and itself. Given an integer N greater than 1, print “PRIME” if it is a prime number, and print “COMPOSITE” otherwise.
|
N = int(input()) is_prime = True for i in range(2, int(N**0.5) + 1): if N % i == 0: is_prime = False break if is_prime: print("PRIME") else: print("COMPOSITE") |
- Reading Input:
- The
input()
function reads a line of text from the user.
- The
int()
function converts this input to an integer and stores it in the variable N
.
- Check for Prime:
is_prime = True
for i in range(2, int(N**0.5) + 1):
if N % i == 0:
is_prime = False
break
- A boolean variable
is_prime
is initialized to True
.
- A
for
loop iterates from 2 to the square root of NNN (inclusive). The square root of NNN is calculated using N**0.5
, and int(N**0.5) + 1
ensures we check all possible divisors.
- If NNN is divisible by any iii in this range,
is_prime
is set to False
, and the loop is broken.
- Print Result:
if is_prime:
print("PRIME")
else:
print("COMPOSITE")
- If
is_prime
remains True
, “PRIME” is printed.
- Otherwise, “COMPOSITE” is printed.