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 (GCC, MinGW) is required.
Usage
Function is_prime(number)
This function returns True
if the number
is prime. Because it uses the C-type unsigned long
for number
it raises a ValueError
if number < 0
or number > cyprimes.max_ulong
. It raises
a TypeError
if number
is not an integer.
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 function, the in operator, subscription
(with integer indexes and slices), is iterable, and can be pickled. It raises a TypeError
if limit
is not an integer.
Methods: All methods raise TypeError
s if their parameters are not integers and ValueError
s
if they are out of range.
-
index(number)
: Returns the index ofnumber
. RaisesValueError
ifnumber
is not prime. -
next(number)
: Returns the next prime number afternumber
orNone
if their is none in the given range. -
previous(number)
: Returns the previous prime number beforenumber
orNone
if their is none in the given range. -
between(start, end)
: Returns atuple
with all prime numbers in the closed interval [start
,end
].
Warning
Although this class uses a bitarray for the sieve be carefull not 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
2020-06-02 (0.4.0)
- First public release
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-03 (0.4.2)
- Fix: README
2020-06-03 (0.4.3)
- Fix: README again :blush:
2020-06-15 (0.5.0)
- Add methods next(), previous(), and between() to class Primes
- Add tests
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.