#prime #primality #number-theory #primal

c-prime

Convenience wrapper for machine-prime

3 releases

new 0.1.2 Jan 4, 2025
0.1.1 Jan 2, 2025
0.1.0 Dec 29, 2024

#1593 in Math

Download history 98/week @ 2024-12-25 289/week @ 2025-01-01

387 downloads per month

CC0 license

21KB
536 lines

C{onvenience}-prime serves to wrap the capabilities of machine-prime in a convenient library.

This library produces primes simply by sequentially checking integers, consequently it is very slow compared to sieving but may be faster if you start at a larger point. e.g iterating through a million primes starting from 0 is going to be slow compared to sieving, but much faster than sieving if you start at 2^64.

Primality

A primality trait is implemented for all primitive datatypes (integers and floats). This trait provides is_prime for general primality and strong_case for slightly faster evaluation of primes, semiprimes and composites with only factors greater than 331.

PrimeSets

Each datatype has a corresponding primeset, prefixed with P. Each primeset can be iterated over, forwards and backwards, checked if they contain a certain number, and calculate their intersection and complement with some other collection (e.g vectors, hashsets...). Users can jump to any random point in the primeset and start iterating from there. Only some set-theoretic functions can be supported as primesets store no actual primes.

Iterators

Primesets are infact syntactic sugar for the Primes iterator, which has identical functionality. The ResiduePrimes iterator is more useful if you are only interested in primes that are members of certain residue classes.

PrimeArrays

Arrays of primes or multiplicative inverses of primes can be constructed at compile time. They are sequential and calculated from the next prime greater than the starting value provided. E.g prime_array::<24>(2) will provide the primes from 3;97 if you want to include 2 you need to start at 1. prime_inv_array::<24>(2) will calculate the multiplicative inverses of the same primes over either 2^128 or 2^64 depending on the datatype. Note that 2 has no multiplicative inverses to any integer datatype, so the function will panic. Both types of arrays have very useful number-theoretic applications, and are generally used as constant values.

Dependencies

~1.5MB
~17K SLoC