filmov
tv
Python for math tutorial_4

Показать описание
A positive integer greater than 1 which has no other factors except 1 and the number itself is called a prime number. 2, 3, 5, 7 etc. are prime numbers as they do not have any other factors. But 12 is not prime (it is composite) since, 4 x 3 = 12.
In this program, we have checked if num is prime or not. Numbers less than or equal to 1 are not prime numbers. Hence, we only proceed if the num is greater than 1.
We check if num is exactly divisible by any number from 2 to num - 1. If we find a factor in that range, the number is not prime, so we set flag to True and break out of the loop.
Outside the loop, we check if flag is True or False.
If it is True, num is not a prime number.
If it is False, num is a prime number.
num = int(input("Enter a number:"))
for x in range (2,num):
if num%x==0:
print("{} is nut a prime number".format(num))
break
else:
print("{} is a prime number".format(num))
In this program, we have checked if num is prime or not. Numbers less than or equal to 1 are not prime numbers. Hence, we only proceed if the num is greater than 1.
We check if num is exactly divisible by any number from 2 to num - 1. If we find a factor in that range, the number is not prime, so we set flag to True and break out of the loop.
Outside the loop, we check if flag is True or False.
If it is True, num is not a prime number.
If it is False, num is a prime number.
num = int(input("Enter a number:"))
for x in range (2,num):
if num%x==0:
print("{} is nut a prime number".format(num))
break
else:
print("{} is a prime number".format(num))