Expand description
Convenience wrapper for machine-prime. Implements primality testing for all primitive types, including floats. Provides iterators and const functions, and modeling of prime sets. Primes here are defined as integers that can only be composed of themselves and a unit of the integers {1,-1}. Therefore numbers with no factor P are primes as well as -P.
use c_prime::{P,Primality,prime_array,prime_inv_array};
// The set of P contains 17
assert_eq!(P.contains(17),true);
// Go to 2^128-1000 and count the primes from there up to 2^128
assert_eq!(P.jump_to(u128::MAX-999).count(),9);
// Count the number of primes under 100
assert_eq!(P.take_while(|x| *x < 100).count(),25);
// Set intersection of the primes and some collection
assert_eq!(P.intersection([2,4,5,6,7]),[2,5,7]);
// The first 25 primes greater than 100
const ODD_PRIMES : [u128;25] = prime_array(100);
// The multiplicative inverses of these primes
const PRIME_INV : [u128;25] = prime_inv_array(100);
// P*P^-1 = 1
for (p,inv) in ODD_PRIMES.iter().zip(PRIME_INV.iter()){
assert_eq!(p.wrapping_mul(*inv),1)
}
// Of course floats can also be primes so long as they can accurately store them
// Here the value is prime so strong case is faster at evaluating it
assert!(4294967291f64.strong_case());
// Another strong case
assert!(!(10570841f64*10570849f64).strong_case());Structs§
- Primes
- Iterator over the set of primes
- Residue
Prime - Iterator of primes of the form X*RING + RESIDUE
Constants§
- P
- Set of all 128-bit Primes
- P8
- Set of all 8-bit primes
- P8I
- Set of all 8-bit signed primes
- P16
- Set of all 16-bit primes
- P32
- Set of all 32-bit signed primes
- P64
- Set of all 64-bit primes
- P16I
- Set of all 16-bit signed primes
- P32F
- Set of all 24-bit primes representable in 64-bit floating-point
- P32I
- Set of all 32-bit signed primes
- P64F
- Set of all 53-bit primes representable in 64-bit floating-point
- P64I
- Set of all 64-bit signed primes
- P128I
- Set of all 128-bit signed primes
Traits§
- Primality
- Trait for primality over all primitive types
Functions§
- next_
prime - Returns the next prime
- prev_
prime - Returns the previous prime
- prime_
array - Initialises an array of sequential 128-bit primes
- prime_
array_ 16 - Initialises an array of sequential 16-bit primes
- prime_
array_ 32 - Initialises an array of sequential 32-bit primes
- prime_
array_ 64 - Initialises an array of sequential 64-bit primes
- prime_
inv_ array - Initialises an array of sequential multiplicative inverses of primes over Z/2^128
- prime_
inv_ array_ 64 - Initialises an array of sequantial multiplicative inverses of primes over Z/2^64