Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
Primes Namespace Reference

Functions

unsigned long next_prime (unsigned long n)
 Find a prime >= n in the database, for hash table sizing.
 
bool check_primes_database ()
 Verify the order of the prime database.
 
bool is_prime (unsigned long n)
 Test if a number is prime using trial division.
 
unsigned long next_prime_number_greater_than (unsigned long n)
 Find the next prime strictly greater than n.
 

Variables

const unsigned long primeList []
 Pre-computed array of prime numbers for fast lookup.
 
const size_t numPrimes = sizeof(primeList) / sizeof(primeList[0])
 Number of primes in the pre-computed database.
 
const unsigned long DefaultPrime = primeList[0]
 Default prime number used when no specific size is requested.
 

Function Documentation

◆ check_primes_database()

bool Primes::check_primes_database ( )

Verify the order of the prime database.

Checks that every number in primeList is greater than the previous one, as the binary search of next_prime() needs. Used for testing and validation.

Returns
true if the entries of primeList are strictly increasing

Definition at line 414 of file primes.C.

References numPrimes, and primeList.

Referenced by main(), and TEST().

◆ is_prime()

bool Primes::is_prime ( unsigned long  n)
inline

Test if a number is prime using trial division.

Checks divisibility by all integers from 2 to sqrt(n).

Parameters
nNumber to test for primality
Returns
true if n is prime, false otherwise
Note
O(sqrt(n)) complexity - use next_prime() for hash table sizing
Example
Primes::is_prime(17); // true
Primes::is_prime(18); // false
Primes::is_prime(2); // true
Primes::is_prime(1); // false
bool is_prime(unsigned long n)
Test if a number is prime using trial division.
Definition primes.H:136

Definition at line 136 of file primes.H.

Referenced by next_prime(), next_prime_number_greater_than(), TEST(), TEST(), TEST(), TEST(), and TEST().

◆ next_prime()

unsigned long Primes::next_prime ( unsigned long  n)

Find a prime >= n in the database, for hash table sizing.

Performs binary search on the pre-computed prime database, which holds only some of the primes. So the result is often larger than the smallest prime >= n: next_prime(2206) is 2309, although 2207 is prime. Below the first entry of the database (101) and above its last one, the result is the smallest prime >= n.

Parameters
nMinimum value for the prime
Returns
The first prime of the database that is >= n; below or above the database, the smallest prime p such that p >= n
Exceptions
std::domain_errorif no prime >= n fits in an unsigned long
Note
Uses pre-computed database, O(log numPrimes) complexity; above it, the prime is found by trial division

Definition at line 383 of file primes.C.

References is_prime(), next_prime_number_greater_than(), and primeList.

Referenced by OhashCommon< HashTbl, Key >::check_resize_before_insert(), Aleph::GenLhashTable< Key, BucketType, Cmp >::insert(), main(), Aleph::GenLhashTable< Key, BucketType, Cmp >::make_default_lists(), hash_dry_detail::prime_table_len(), OhashCommon< HashTbl, Key >::rebuild_capacity(), Aleph::GenLhashTable< Key, BucketType, Cmp >::remove(), Aleph::GenLhashTable< Key, BucketType, Cmp >::search_or_insert(), OhashCommon< HashTbl, Key >::shrink_after_removal(), TEST(), TEST(), and TEST().

◆ next_prime_number_greater_than()

unsigned long Primes::next_prime_number_greater_than ( unsigned long  n)
inline

Find the next prime strictly greater than n.

Unlike next_prime() which uses a database, this function computes primes dynamically using trial division. Useful when the database doesn't contain large enough primes.

Parameters
nFind prime greater than this value
Returns
Smallest prime p such that p > n
Exceptions
std::domain_errorif n is too large (would overflow)
Note
O(sqrt(p) * (p-n)/2) worst case - slower than next_prime()
Example
unsigned long next_prime_number_greater_than(unsigned long n)
Find the next prime strictly greater than n.
Definition primes.H:171

Definition at line 171 of file primes.H.

References ah_domain_error_if, is_prime(), and max().

Referenced by next_prime(), and TEST().

Variable Documentation

◆ DefaultPrime

const unsigned long Primes::DefaultPrime = primeList[0]

◆ numPrimes

const size_t Primes::numPrimes = sizeof(primeList) / sizeof(primeList[0])

Number of primes in the pre-computed database.

Definition at line 379 of file primes.C.

Referenced by check_primes_database(), TEST(), TEST(), TEST(), and TEST().

◆ primeList

const unsigned long Primes::primeList

Pre-computed array of prime numbers for fast lookup.

Definition at line 45 of file primes.C.

Referenced by check_primes_database(), next_prime(), TEST(), TEST(), TEST(), and TEST().