Skip to main content

Module for working with prime numbers.

Project description

CyPrimes

Module for working with prime numbers written in Cython.

This is just a toy project to try out Cython.

Installation

# pip install cyprimes

To install CyPrimes a C-compiler (e.g. GCC), which must support ISO C99, is required.

Usage

Function is_prime(number)

Returns True if the number is prime. Because it uses the C-type unsigned long long (C99) for number it raises a ValueError if number < 0 or number > cyprimes.max_ulong. It raises a TypeError if number is not an integer.

Function next_prime(number):

Returns the next prime number after number.

Function previous_prime(number)

Returns the previous prime number before number.

Function primes_between(start, end)

Returns a tuple with all prime numbers in the closed interval [start, end].

Class Primes(limit)

This class uses the Sieve of Eratosthenes to find all prime numbers up to limit.

An instance of this class supports the len and sys.getsizeof functions, the in and == operators, subscription (with integer indexes and slices), is iterable, and can be pickled. Two Primes objects are considered equal if they contain the same prime numbers. A TypeError will be raised if limit is not an integer.

Methods: All methods raise TypeErrors if their parameters are not integers and ValueErrors if they are out of range.

  • index(number): Returns the index of number. Raises ValueError if number is not prime.

  • next(number): Returns the next prime number after number or None if their is none in the given range.

  • previous(number): Returns the previous prime number before number or None if their is none in the given range.

  • between(start, end): Returns a tuple with all prime numbers in the closed interval [start, end].

Warning

Although this class uses a bitarray for the sieve, be carefull not to run out of memory:

limit size
100 7 B
1 000 63 B
10 000 625 B
100 000 6 KiB
1 000 000 61 KiB
10 000 000 610 KiB
100 000 000 6 MiB
1 000 000 000 60 MiB
10 000 000 000 596 MiB
100 000 000 000 6 GiB
1 000 000 000 000 58 GiB

History

2022-04-13 (0.7.6)

  • Now works with Python 3.10

2021-11-29 (0.7.5)

  • Fix method Primes.__eq__()

2021-06-03 (0.7.4)

  • Bugfix: should now work correctly on 32-bit systems

2020-12-31 (0.7.3)

  • No changes

2020-07-28 (0.7.2)

  • Correct version in generated C code

2020-07-28 (0.7.1)

  • Add checks to avoid overflow

2020-07-27 (0.7.0)

  • Add functions next_prime(), previous_prime(), and primes_between()

2020-06-25 (0.6.0)

  • Add methods __eq__(), __hash__(), __sizeof__() ,and __repr__() to class Primes

2020-06-15 (0.5.0)

  • Add methods next(), previous(), and between() to class Primes
  • Add tests

2020-06-03 (0.4.3)

  • Fix: README again :blush:

2020-06-03 (0.4.2)

  • Fix: README

2020-06-03 (0.4.1)

  • Fix: Primes() and is_prime() now raise TypeErrors if the parameter is not an integer
  • Fix: Primes.__reversed__() now works correct

2020-06-02 (0.4.0)

  • First public release

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

cyprimes-0.7.6.tar.gz (74.9 kB view details)

Uploaded Source

File details

Details for the file cyprimes-0.7.6.tar.gz.

File metadata

  • Download URL: cyprimes-0.7.6.tar.gz
  • Upload date:
  • Size: 74.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.8.1 pkginfo/1.8.2 requests/2.27.1 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.10.2

File hashes

Hashes for cyprimes-0.7.6.tar.gz
Algorithm Hash digest
SHA256 34f651c1e4b797d303ff78375b9a66dad442eb16a8043f93d363c23e6790cb31
MD5 c8e31ee2986c004077db1ac2b600ee31
BLAKE2b-256 0e247cb99957fc3fa0627b8c01f082492977813d131a30cb245af124110c1d1e

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page