python判断素数程序
什么是质数? (What is a prime number?)
A prime number is a natural number that is greater than 1 and cannot be formed by multiplying two smaller natural numbers.
质数是大于1的自然数,不能通过将两个较小的自然数相乘而形成。
Given a number num, we have to check whether num is a prime number or not.
给定数字num ,我们必须检查num是否是质数。
Example:
例:
Input:
num = 59
Output:
59 is a prime number
Input:
num = 123
Output:
123 is not a prime number
程序检查Python中的素数 (Program to check prime number in Python)
# Python program to check prime number
# Function to check prime number
def isPrime(n):
return all([(n % j) for j in range(2, int(n/2)+1)]) and n>1
# Main code
num = 59
if isPrime(num):
print(num, "is a prime number")
else:
print(num, "is not a prime number")
num = 7
if isPrime(num):
print(num, "is a prime number")
else:
print(num, "is not a prime number")
num = 123
if isPrime(num):
print(num, "is a prime number")
else:
print(num, "is not a prime number")
Output
输出量
59 is a prime number
7 is a prime number
123 is not a prime number
翻译自: https://www.includehelp.com/python/check-prime-number.aspx
python判断素数程序