跳转到主要内容

未提供项目描述

项目描述

primesieve-python

Build Status PyPI

摘要

用于primesieve C++库的Python绑定。

比任何纯Python代码快几个数量级地生成质数!

功能

  • 获取质数数组
  • 使用少量内存遍历质数
  • 找到第n个质数
  • 计数/打印质数和质数k元组
  • 多线程用于计数质数和找到第n个质数
  • NumPy支持

临时构建命令

正常命令

python3 setup.py test  && \
python3 setup.py install --user

现在应该可以工作了,但您可能需要回退到

python3 -m pip install --requirement requirements.txt --no-build-isolation . |& tee ~/y0.txt

先决条件

我们为Windows、macOS和Linux的x86和x64 CPU提供primesieve轮子(分发包)。对于其他操作系统和/或CPU,您需要安装一个C++编译器。

# Ubuntu/Debian
sudo apt install g++ python-dev

# Fedora
sudo dnf install gcc-c++ python-devel

# macOS
xcode-select --install

安装

# Python 3.5 or later
pip install primesieve

# For Python 2.7 use:
pip install "primesieve<=1.4.4"

Conda安装

TravisCI AppVeyor Circle CI Anaconda-Server Badge

使用Conda安装python-primesieve时,不需要安装C++编译器。

conda install -c conda-forge python-primesieve

用法示例

>>> from primesieve import *

# Get an array of the primes <= 40
>>>  primes(40)
[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37]

# Get an array of the primes between 100 and 120
>>>  primes(100, 120)
[101, 103, 107, 109, 113]

# Get an array of the first 10 primes
>>>  n_primes(10)
[2, 3, 5, 7, 11, 13, 17, 19, 23, 29]

# Get an array of the first 10 primes >= 1000
>>>  n_primes(10, 1000)
[1009, 1013, 1019, 1021, 1031, 1033, 1039, 1049, 1051, 1061]

# Get the 10th prime
>>> nth_prime(10)
29

# Count the primes below 10**9
>>> count_primes(10**9)
50847534

这里是所有可用函数的列表。

遍历素数

除了生成一个大的素数数组然后再对素数进行操作外,也可以直接遍历素数,这样可以节省内存。

>>> import primesieve

it = primesieve.Iterator()
prime = it.next_prime()

# Iterate over the primes below 10000
while prime < 10000:
    print prime
    prime = it.next_prime()

# Set iterator start number to 100
it.skipto(100)
prime = it.prev_prime()

# Iterate backwards over the primes below 100
while prime > 0:
    print prime
    prime = it.prev_prime()

NumPy支持

使用primesieve.numpy模块,你可以使用本机C++性能生成素数数组!

相比之下,primesieve模块生成素数数组大约慢3倍,主要是因为将C素数数组转换为Python数组相当慢。

>>> from primesieve.numpy import *

# Generate a numpy array with the primes below 100
>>>  primes(100)
array([ 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])

# Generate a numpy array with the first 100 primes
>>>  n_primes(100)
array([  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, 101,
       103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167,
       173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239,
       241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313,
       317, 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397,
       401, 409, 419, 421, 431, 433, 439, 443, 449, 457, 461, 463, 467,
       479, 487, 491, 499, 503, 509, 521, 523, 541])

开发

您需要安装一个C++编译器,请参阅先决条件

# Install prerequisites
pip install cython pytest numpy

# Clone repository
git clone --recursive https://github.com/kimwalisch/primesieve-python

cd primesieve-python

# Build and install primesieve-python
pip install . --upgrade

# Run tests
pytest

如何进行新版本发布

  • 您需要是primesieve-python仓库的维护者。
  • 您需要是primesieve pypi项目的维护者。
  • .travis.ymlcibuildwheel#example-setup进行比较,并在需要时更新.travis.yml
  • setup.py中更新支持的Python版本(我们支持的版本与cibuildwheel相同)。
  • setup.py中增加primesieve-python版本。理想情况下,这应该是发布前的最后一个提交,因为这会将新的primesieve轮子上传到https://test.pypi.org
  • 检查所有primesieve轮子(Windows、macOS、Linux)是否已上传到https://test.pypi.org
  • 如果没有,请阅读Travis CI日志并修复错误。
  • 最后,在GitHub上发布新版本。

项目详情


下载文件

下载适合您平台的文件。如果您不确定选择哪个,请了解更多关于安装包的信息。

源分布

primesieve-2.3.4.tar.gz (146.8 kB 查看散列值)

上传时间

支持者:

AWS AWS 云计算和安全赞助商 Datadog Datadog 监控 Fastly Fastly CDN Google Google 下载分析 Microsoft Microsoft PSF 赞助商 Pingdom Pingdom 监控 Sentry Sentry 错误日志 StatusPage StatusPage 状态页面