Kinh Nghiệm về How do you get a prime number in a while loop in python? 2022
Hoàng Thị Bích Ngọc đang tìm kiếm từ khóa How do you get a prime number in a while loop in python? được Cập Nhật vào lúc : 2022-09-27 19:24:07 . Với phương châm chia sẻ Kinh Nghiệm Hướng dẫn trong nội dung bài viết một cách Chi Tiết 2022. Nếu sau khi đọc tài liệu vẫn ko hiểu thì hoàn toàn có thể lại Comment ở cuối bài để Ad lý giải và hướng dẫn lại nha.In this post, we will write a program in Python to check whether the input number is prime or not using while loop.
Nội dung chính- How to write Prime number Program in Python?Example 1: Using a flag variableExample 2: Using a
for...else statementHow do you find a prime number in a while loop?How do you get primes in Python?How do you print a prime number in Python?Is prime () in Python?
How to write Prime number Program in Python?
- Prime number is a number that is greater than 1 and divided by 1 or itself.In other words, prime numbers can’t be divided by other numbers than itself or 1.For example- 2, 3, 5, 7, 11, 13, 17, 19, 23…. are the prime numbers.Let’s see the prime number program in Python.
In this Python program, we will take an input from the user and check whether the number is prime or not.
num=int(input("Enter a number ( greater than 1)"))
f=0
i=2
whilei<= num/2:
ifnum%i==0:
f=1
break
i=i+1
iff==0:
print("The entered number is a PRIME number")
else:
print("The entered number is not a PRIME number")
Output:
Enteranumber:19
TheenterednumberisaPRIMEnumber
Example to check whether an integer is a prime number or not using for loop and if...else statement. If the number is not prime, it's explained in output why it is not a prime number.
To understand this example, you should have the knowledge of the following Python programming topics:
- Python
if...else StatementPython for LoopPython break and continue
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 6 is not prime (it is composite) since, 2 x 3 = 6.
Example 1: Using a flag variable
# Program to check if a number is prime or not num = 29 # To take input from the user #num = int(input("Enter a number: ")) # define a flag variable flag = False # prime numbers are greater than 1 if num > 1: # check for factors for i in range(2, num): if (num % i) == 0: # if factor is found, set flag to True flag = True # break out of loop break # check if flag is True if flag: print(num, "is not a prime number") else: print(num, "is a prime number")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.
Note: We can improve our program by decreasing the range of numbers where we look for factors.
In the above program, our search range is from 2 to num - 1.
We could have used the range, range(2,num//2) or range(2,math.floor(math.sqrt(num)+1)). The latter range is based on the fact that a composite number must have a factor less than or equal to the square root of that number. Otherwise, the number is prime.
You can change the value of variable num in the above source code to check whether a number is prime or not for other integers.
In Python, we can also use the for...else statement to do this task without using an additional flag variable.
Example 2: Using a for...else statement
# Program to check if a number is prime or not num = 407 # To take input from the user #num = int(input("Enter a number: ")) # prime numbers are greater than 1 if num > 1: # check for factors for i in range(2,num): if (num % i) == 0: print(num,"is not a prime number") print(i,"times",num//i,"is",num) break else: print(num,"is a prime number") # if input number is less than # or equal to 1, it is not prime else: print(num,"is not a prime number")Output
407 is not a prime number 11 times 37 is 407Here, we have used a for..else statement to check if num is prime.
It works on the logic that the else clause of the for loop runs if and only if we don't break out the for loop. That condition is met only when no factors are found, which means that the given number is prime.
So, in the else clause, we print that the number is prime.