欧拉计划启发的数论相关函数库。
eulerlib的Python项目详细描述
eulerlib是一个与休闲数学和数论相关的图书馆 受Project Euler启发的函数。可用功能包括:
- 质数生成
- 除数函数(西格玛函数)
- 欧拉总函数
- 使用euclid算法的最大公约数(gcd)
- 最小公倍数(LCM)
- 整数平方根
- 斐波那契数
- 泛数字
- 回文数
- 毕达哥拉斯三胞胎
此库中的函数可用于求解休闲数学和 规划问题,如欧拉工程问题。
安装
eulerlib可以通过python包索引(PyPI)使用 pip。
>>> pip install --upgrade eulerlib
使用pip卸载。
>>> pip uninstall eulerlib
用法
在python控制台中,您可以根据需要从eulerlib导入函数/类。
>>> from eulerlib import primes >>> p10 = primes(10) >>> print(p10) [2, 3, 5, 7]
divisors类实现与素数分解相关的函数, 西格玛函数等
>>> from eulerlib import Divisors >>> mydiv = Divisors(10000) >>> div84 = mydiv.divisors(84) #divisors of 84 >>> print(div84) [1, 2, 3, 4, 6, 7, 12, 14, 21, 28, 42, 84] >>> pf840 = mydiv.prime_factors(840) # prime factors of 840 >>> print(pf840) [(2, 3), (3, 1), (5, 1), (7, 1)]
示例:已解决Project Eulerproblem 3。
from eulerlib import is_square, primes #get approximate square root of number since #largest prime factor < sq. root (is_sq, sqroot) = is_square(600851475143L) #get a list of primes less than the approx. square root. test_primes = primes(sqroot + 1L) #test the primes from the list to find the largest factor len_p = len(test_primes) for i in range(1,len_p+1): j = 0 - i test_fact = test_primes[j] if 600851475143L%test_fact == 0: break answer = test_fact #Set the answer
请参阅完整的documentation。
模块
prime_numbers.py | Functions to generate lists of primes. |
numtheory.py |
|
fibonacci.py | Functions related to the Fibonacci sequence. |
pythagoras.py | Functions related to Pythagorean triples. |
etc.py | Miscellaneous functions:
|
开发
源代码存储库(GitHub,BitBucket)可用。 Bug reports和建议是最受欢迎的。
许可证
eulerlib是根据Apache License 2.0授权的。