# A prime number or composite

The user enters a number. The program must determine whether it is a prime number or composite.

**Note.** A prime number are natural numbers greater than 1, which are divided only by 1 and themselves.

```
from math import sqrt
n = 0
while n < 2:
n = int(input())
prime = True
i = 2
while i <= sqrt(n):
if n % i == 0:
prime = False
break
i += 1
if prime:
print("It's a prime number")
else:
print("This is a composite number")
```

With comments:

```
# Import the sqrt() function from the math module.
# The sqrt() function takes the square root.
from math import sqrt
# To avoid errors in the program, the user
# must enter a number greater than one.
n = 0
while n < 2:
n = int(input())
# We assume that a prime number has been entered.
prime = True
# 2 is the first possible divisor of the number
i = 2
# We have to iterate over all possible divisors
# up to the square root of the number.
while i <= sqrt(n):
# If the number is divisible by the current divisor
# without a remainder, then it cannot be prime.
if n % i == 0:
# Assumption that the number is prime is incorrect.
prime = False
# We stop the loop with the break statement, since
# checking subsequent divisors does not make sense.
break
# Next divisor
i += 1
if prime: # if prime == True
print("It's a prime number")
else: # when prime == False
print("This is a composite number")
```

Example of execution:

5003
It's a prime number

```
from math import sqrt
n = 0
while n < 2:
n = int(input())
i = 2
while i <= sqrt(n):
if n % i == 0:
print("Составное число")
break
i += 1
else:
print("Простое число")
```

```
from math import sqrt
def is_prime(n):
i = 2
while i <= sqrt(n):
if n % i == 0:
return False
i += 1
if n > 1:
return True
a = int(input())
if is_prime(a):
print("Prime")
else:
print("Not prime")
```

```
from math import sqrt
n = int(input())
prime = True
i = 2
while i <= sqrt(n) and prime is True:
if n % i == 0:
prime = False
i += 1
if prime:
print("Prime")
else:
print("Not prime")
```