Python Prime Number Check

Document 对象参考手册Python3 Examples

A natural number greater than 1, apart from 1 and itself, cannot be divided by other natural numbers (prime numbers) (2, 3, 5, 7, etc.). In other words, the number has no other factors besides 1 and itself.


test.py file:

# -*- coding: UTF-8 -*- # Filename : test.py # author by : www.example.com # Python program to detect whether the number entered by the user is a prime number # User enters a number num = int(input("Please 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,"multiplied by",num//i,"Yes",num) break else: print(num,"is a prime number") # If the input number is less than or equal to 1, it is not a prime number else: print(num,"is not a prime number")

The output after executing the above code is:

$ python3 test.py 
请输入一个数字: 1
1 不是质数
$ python3 test.py 
请输入一个数字: 4
4 不是质数
2 乘于 2 是 4
$ python3 test.py 
请输入一个数字: 5
5 是质数

Document 对象参考手册Python3 Examples

Other Extensions