python判断素数程序_Python程序检查素数
生活随笔
收集整理的這篇文章主要介紹了
python判断素数程序_Python程序检查素数
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
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 = 59Output:59 is a prime numberInput:num = 123Output: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判斷素數程序
總結
以上是生活随笔為你收集整理的python判断素数程序_Python程序检查素数的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Java ArrayList get()
- 下一篇: [转载] python学习笔记