Python中的 PrimePy 模块
素数是大于 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))
Python3
# Importing primes function
# From primePy Library
from primePy import primes
a = primes.factor(15)
print(a)
a = primes.factor(75689456252)
print(a)
Python3
# Importing primes function
# From primePy Library
from primePy import primes
a = primes.factors(774177)
print(a)
a = primes.factors(15)
print(a)
Python3
# Importing primes function
# From primePy Library
from primePy import primes
a = primes.first(5)
print(a)
a = primes.first(10)
print(a)
Python3
# Importing primes function
# From primePy Library
from primePy import primes
a = primes.upto(17)
print(a)
a = primes.upto(100)
print(a)
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)
Python3
# Importing primes function
# From primePy Library
from primePy import primes
a = primes.phi(5)
print(a)
a = primes.phi(10)
print(a)
输出:
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