素數是大於 1 的自然數,其唯一因數是 1 和該數本身。 2 是唯一的偶數素數。我們可以用“6n+1”或“6n-1”(2 和 3 除外)表示任何素數,其中 n 是自然數。
primePy 是 Python 庫,用於計算與素數相關的運算。借助 primePy 模塊的函數,它將在更短的時間內執行所有函數。
安裝庫
該模塊不是 Python 內置的。您需要在外部安裝它。要安裝此模塊,請在終端中鍵入以下命令。
pip install primePy
primePy 的函數
1. primes.check(n):如果‘n’是素數,則返回True,否則返回False。
例子:
Python3
# Importing primes function
# From primePy Library
from primePy import primes
print(primes.check(105))
print(primes.check(71))
輸出:
False True
2. primes.factor(n):返回‘n’的最小質因數。
例子:
Python3
# Importing primes function
# From primePy Library
from primePy import primes
a = primes.factor(15)
print(a)
a = primes.factor(75689456252)
print(a)
輸出:
3 2
3. primes.factors(n):返回‘n’的所有素因數,如果存在重複因數。
例子:
Python3
# Importing primes function
# From primePy Library
from primePy import primes
a = primes.factors(774177)
print(a)
a = primes.factors(15)
print(a)
輸出:
[3, 151, 1709] [3, 5]
4. primes.first(n):它將返回第一個‘n’素數。
例子:
Python3
# Importing primes function
# From primePy Library
from primePy import primes
a = primes.first(5)
print(a)
a = primes.first(10)
print(a)
輸出:
[2, 3, 5, 7, 11] [2, 3, 5, 7, 11, 13, 17, 19, 23, 29]
5. primes.upto(n):返回所有小於或等於‘n’的素數。
例子:
Python3
# Importing primes function
# From primePy Library
from primePy import primes
a = primes.upto(17)
print(a)
a = primes.upto(100)
print(a)
輸出:
[2, 3, 5, 7, 11, 13, 17]
[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97]
6. primes. Between(m, n):返回m到n之間的所有素數。
例子:
Python3
# Importing primes function
# From primePy Library
from primePy import primes
a = primes.between(4, 15)
print(a)
a = primes.between(25, 75)
print(a)
輸出:
[5, 7, 11, 13] [29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73]
7. primes.phi(n):返回小於‘n’且與n無公因數的整數個數。
例子:
Python3
# Importing primes function
# From primePy Library
from primePy import primes
a = primes.phi(5)
print(a)
a = primes.phi(10)
print(a)
輸出:
4 4
相關用法
- Python PIL ImageGrab.grab()用法及代碼示例
- Python PIL ImageGrab.grabclipboard()用法及代碼示例
- Python Pandas.apply()用法及代碼示例
- Python Pandas.Categorical()用法及代碼示例
- Python Pandas.CategoricalDtype()用法及代碼示例
- Python Pandas DataFrame.abs()用法及代碼示例
- Python Pandas dataframe.add()用法及代碼示例
- Python Pandas dataframe.add_prefix()用法及代碼示例
- Python Pandas dataframe.add_suffix()用法及代碼示例
- Python Pandas dataframe.aggregate()用法及代碼示例
- Python Pandas dataframe.all()用法及代碼示例
- Python Pandas dataframe.applymap()用法及代碼示例
- Python Pandas dataframe.asfreq()用法及代碼示例
- Python Pandas dataframe.assign()用法及代碼示例
- Python Pandas DataFrame.astype()用法及代碼示例
- Python Pandas Dataframe.at[ ]用法及代碼示例
- Python Pandas dataframe.at_time()用法及代碼示例
- Python Pandas DataFrame.axes用法及代碼示例
- Python Pandas dataframe.between_time()用法及代碼示例
- Python Pandas dataframe.bfill()用法及代碼示例
- Python Pandas DataFrame.blocks用法及代碼示例
- Python Pandas dataframe.clip()用法及代碼示例
- Python Pandas dataframe.clip_lower()用法及代碼示例
- Python Pandas dataframe.clip_upper()用法及代碼示例
- Python Pandas DataFrame.columns用法及代碼示例
注:本文由純淨天空篩選整理自vasu_gupta大神的英文原創作品 PrimePy module in Python。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。