• Stars
    star
    133
  • Rank 272,600 (Top 6 %)
  • Language Cython
  • License
    MIT License
  • Created over 9 years ago
  • Updated 4 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Fast prime number generator. Python bindings for the primesieve C++ library

primesieve-python

Build Status PyPI

Summary

Python bindings for the primesieve C++ library.

Generates primes orders of magnitude faster than any pure Python code!

Features:

  • Get an array of primes
  • Iterate over primes using little memory
  • Find the nth prime
  • Count/print primes and prime k-tuplets
  • Multi-threaded for counting primes and finding the nth prime
  • NumPy support

Prerequisites

We provide primesieve wheels (distribution packages) for Windows, macOS and Linux for x86 and x64 CPUs. For other operating systems and/or CPUs you need to have installed a C++ compiler.

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

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

# macOS
xcode-select --install

Installation

# Python 3.5 or later
pip install primesieve

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

Conda Installation

TravisCI AppVeyor Circle CI Anaconda-Server Badge

You don't need to install a C++ compiler when installing python-primesieve using Conda.

conda install -c conda-forge python-primesieve

Usage examples

>>> 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

Here is a list of all available functions.

Iterating over primes

Instead of generating a large array of primes and then do something with the primes it is also possible to simply iterate over the primes which uses less memory.

>>> 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 support

Using the primesieve.numpy module you can generate an array of primes using native C++ performance!

In comparison the primesieve module generates an array of primes about 3 times slower mostly because the conversion of the C primes array into a python array is quite slow.

>>> 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])

Development

You need to have installed a C++ compiler, see Prerequisites.

# 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

How to do a new release

  • You need to be a maintainer of the primesieve-python repo.
  • You need to be a maintainer of the primesieve pypi project.
  • Compare .travis.yml with cibuildwheel#example-setup and update .travis.yml if needed.
  • Update the supported Python versions in setup.py (we support the same versions as cibuildwheel).
  • Increment the primesieve-python version in setup.py. Ideally this should be the last commit before the release as this uploads the new primesieve wheels to https://test.pypi.org.
  • Check if all primesieve wheels (Windows, macOS, Linux) have been uploaded to https://test.pypi.org.
  • If not, read the Travis CI logs and fix the bugs.
  • Finally, do a new release on GitHub.

More Repositories

1

PySolFC

A comprehensive, feature-rich, open source, and portable, collection of Solitaire games.
Python
449
star
2

fortune-mod

Implementation of the Unix fortune command for displaying a random quotation, for Linux and other systems.
C
332
star
3

what-i-learned-from-porting-to-freebsd

What I Learned from porting my projects to FreeBSD
115
star
4

fc-solve

Freecell Solver - a C library for automatically solving Freecell and some other variants of card Solitaire
C
56
star
5

Freenode-programming-channel-FAQ

A Frequently Asked Questions List of the Freenode ##programming Channel.
Python
46
star
6

Bezier-Envelope-for-Inkscape

The Bezier Envelope Extension for Inkscape
Python
28
star
7

catable

CATAlyst BLog Engine - written by perl cats
Perl
17
star
8

what-you-should-know-about-automated-testing

What you should know about automated software testing
Perl
14
star
9

testing-static-sites

Ideas for automated tests of static web sites
Makefile
14
star
10

perl-XML-LibXML

The XML-LibXML CPAN Distribution for Processing XML using the libxml2 library
Perl
14
star
11

gtk3-cmake-examples

gtk3 and cmake examples
CMake
13
star
12

how-to-share-code-online

How to share source code on online forums in order to get help with it
12
star
13

shlomi-fish-homepage

Shlomi Fish’s Homepage Sources - www.shlomifish.org
HTML
11
star
14

mastering-perl

Previously a copy of the book Mastering Perl as published by O'Reilly. Now Removed.
11
star
15

perl5-for-JavaScript

microperl fork (http://themucker.github.io/plu/)
C
9
star
16

perl-XML-RSS

The Repository for the CPAN XML::RSS module for parsing and generating various versions of RSS.
Perl
9
star
17

Summerschool-at-the-NSA

"Summerschool at the NSA" - a screenplay. Two Hollywood actresses in 2013 conspire to kick the ass of the National Security Agency (NSA), using special warfare that is completely non-violent.
Shell
8
star
18

shlomif-computer-settings

Shlomi Fish's personal software settings/dot-files/etc.
Shell
7
star
19

PySolFC-Cardsets

PySolFC Cardsets repository
7
star
20

perl5-for-JavaScript--take2

perl5 for JS via Emscripten.
C
6
star
21

perl-Config-IniFiles

The Config-IniFiles CPAN Distribution
Perl
6
star
22

rinutils

C11 / gnu11 utilities C library by Shlomi Fish / Rindolf
C
5
star
23

shlomif-snippets

My Collection of various short, exploratory or demonstrative code files in Perl, Python and other langs
Perl
5
star
24

gringotts

A Secure Notes Manager
C
5
star
25

Inline-Ruby

Embed and interact with Ruby code from within Perl 5
Perl
5
star
26

rebookmaker

Generate EPUBs from a .json metadata file and input HTML pages. A FOSS rewrite of setanta's "ebookmaker"
Python
5
star
27

black-hole-solitaire

Solvers and statistics for “Golf” solitaire, “Black Hole” solitaire, “All in a Row” solitaire and related card patience games.
Perl
5
star
28

my-real-person-fan-fiction

Why real person fanfiction could "hurt" actors but not singers, and what to do about it
Makefile
5
star
29

perl-graph-easy

cpan
Perl
4
star
30

vim-begin

The Vim Beginners’ Site
SCSS
4
star
31

app-notifier

HTTP Client/Server For Desktop Notifications
Perl
4
star
32

Text-Table

CPAN Distribution to render text / ASCII-art / Unicode tables
Perl
4
star
33

pysol_cards

Python library for dealing cards like PySol FC, MS Freecell/Freecell Pro, or PySol legacy
Python
4
star
34

shlomif-tech-diary

HTML
4
star
35

Human-Hacking-Field-Guide

A novella titled “The Human Hacking Field Guide” about teenage open source enthusiasts near Los Angeles, California
Shell
4
star
36

emcc-array-of-pointers-example

emcc array of C strings / C pointers example.
JavaScript
4
star
37

KlondikeSolver

Solitaire solver implementation for Klondike
C++
4
star
38

binary-puzzle-garden

Various programs to manipulate the so-called "Binary Puzzle"s which are featured on http://www.binarypuzzle.com/
Perl
4
star
39

docmake

Docmake is an automated builder from DocBook/XML to its output formats (e.g XHTML5 or PDF). It aims to be a saner replacement for xmlto.
Perl
4
star
40

perl-Statistics-Descriptive

The Statistics::Descriptive Perl 5/CPAN distribution
Perl
4
star
41

Graph-Easy-As_svg

The CPAN Distribution
Perl
3
star
42

Humanity-the-Movie

Sources for the screenplay for the Humanity (the Movie) - a parody about Humanity and modern life in particular.
Shell
3
star
43

vim-freecell

Vim helpers for viewing/editing freecell solitaire layouts and solutions
Vim Script
3
star
44

Fiction-XML

Fiction-Text, Screenplay-Text, Fiction-XML and Screenplay-XML.
Perl
3
star
45

Buffy-a-Few-Good-Slayers

A screenplay for a Buffy fanfiction set in 2014/2015 in a forked version of the Buffyverse.
Shell
3
star
46

perl-File-Dir-Dumper

Dump nested directory structures to a stream.
Perl
3
star
47

patsolve

A solver for Freecell and Seahaven Towers
C
3
star
48

abc-path

Solvers, Generators, etc. for the game ABC Path
JavaScript
3
star
49

perl-XML-LibXSLT

XSLT
3
star
50

writing-the-perfect-question

Jon Skeet's Writing the Perfect Question
Makefile
3
star
51

pysol-sound-server

pysol-sound-server to provide sounds and music for PySol FC. Maintenance fork with support for python 3 and future enhancements
C
3
star
52

pysolfc-website

sources for the pysolfc web site
HTML
2
star
53

better-scm

Sources of the Better-SCM Site
Perl
2
star
54

Math-Cephes

A Perl/CPAN Interface to the Cephes Mathematics Library
C
2
star
55

xxHash-wrapper

A wrapper for xxHash with some cmake glue
C
2
star
56

zenfilter

Small Python script used to filter TravisCI output
Python
2
star
57

python-vnu_validator

vnu validator wrapper
HTML
2
star
58

vim-screenplay-text

Vim Script
2
star
59

perl-begin

The Perl Beginners’ Site’ Sources
HTML
2
star
60

vim-faq-notes

2
star
61

Adrian-Ettlinger-Emails-Collection

Perl
2
star
62

HTML-Spelling-Site

Spell checking system/framework for an entire static HTML site.
Perl
2
star
63

shlomif-cmake-modules

CMake
2
star
64

test-xml-ordered

Perl
2
star
65

putting-cards-2019-2020

Summary of Rindolfism, Shlomi Fish's personal and dynamic philosophy, as of 2019-2020
Perl
2
star
66

Shlomi-Fish-Back-to-my-Homepage-Logo

The new incarnation of the Back to My Homepage Logo Based on the Logo from the film Back to the Future
Python
2
star
67

captioned-image--emma-watson-doesnt-need-a-wand

Captioned Image: Emma Watson played Hermione in the Harry Potters but she doesn't need a wand to kick your ass.
Makefile
2
star
68

fc-solve-site-assets

Freecell Solver's Site Assets (tarballs, downloads, etc.)
Ruby
2
star
69

quad-pres

Perl
2
star
70

SQL-Injections--danger-and-prevention

SQL Injections
Ruby
2
star
71

Docker-CLI-Wrapper

wrapper for the Docker CLI
Perl
2
star
72

Selina-Mandrake

Selina Mandrake - The Slayer: “caught between post-modernism and the New Age” - a screenplay
Shell
2
star
73

SQL-Abstract

The CPAN SQL::Abstract Module
Perl
2
star
74

MeToo-me-too

#MeToo me too! The collection of "sexual harassment" quotes where we show that sexy women (and men) are competent and respected
Makefile
2
star
75

pydistman

python "overkill DRY" distribution manager
Python
2
star
76

rakudo-git-master-install

Bash program to install rakudo perl6 git master
Shell
2
star
77

freecell-pro-0fc-deals

Finding zero freecells deals in the fc-pro range
Shell
2
star
78

perl-html-links-localize

Perl
2
star
79

cpan-smoke-auto-setup

Perl
2
star
80

html-tutorial

CSS
2
star
81

dzil-dist-ini-format-documentation

Proposed draft for Dist-Zilla's dist.ini's format document
2
star
82

shlomif-c-snippets

C (and some C++) snippets and short demonstration programs by Shlomi Fish and Friends
C++
2
star
83

Captioned-Image-Ayn-Rand-to-Christina-Grimmie

A captioned image reading "If Ayn Rand was born in the 1990s, she would be Christina Grimmie.".
Makefile
2
star
84

Star-Trek--We-the-Living-Dead

Star Trek Deep Space Nine: “We, the Living Dead” - a fan fiction screenplay by Shlomi Fish
Shell
2
star
85

cpandeps

The code behind deps.cpantesters.org
Perl
2
star
86

add-to-word-search.vim

Add more keywords to the search in Vim
Vim Script
2
star
87

plea-to-not-use-github-tags-as-releases

A plea (from packagers/etc.) to not use GitHub tags as Releases and instead make regular release tarballs.
2
star
88

usr-share-games-fortunes--backup

/usr/share/games/fortunes backup
Shell
2
star
89

cookiecutter--shlomif-latemp-sites

Common files and skeleton for Shlomi Fish's Latemp sites
XSLT
2
star
90

missile-vs-melee-take-three

"Queen Amidala vs. The Klingon Warriors" - an enhanced/modernised version of the David and Goliath story set in a fanfic crossoverred Star Trek and Star Wars universe. "Why can't we have both?" (CC-by)
Perl
2
star
91

peter-sanders-priority-queues

C
2
star
92

perl-HTML-Widgets-NavMenu

Perl
2
star
93

The-Muppets-Show--The-New-Incarnation

New incarnation of The Muppet Show with themes such as Summer Glau/Chuck Norris/Grammar nazism ; Jenn Lawrence/SLP/Hunger Games ; Harry Potter ; etc.
Shell
2
star
94

urpmi

The Mageia Linux Software Installation and Management Tools
Perl
1
star
95

book-of-proof--exercises-solutions

Solutions by Shlomi Fish for the Exercises in the book "Book of Proof".
TeX
1
star
96

perl-Code-TidyAll-Plugin-ClangFormat

clang-format plugin for Code-TidyAll
Perl
1
star
97

lm-solve-layouts

1
star
98

Captioned-Image-If-You-Wanna-Be-Sad

A captioned image from The Simpsons
Makefile
1
star
99

perl-Parallel-ForkManager-Segmented

perl-Parallel-ForkManager-Segmented
Perl
1
star
100

the-enemy

HTML
1
star