Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
primes.H
Go to the documentation of this file.
1
2/*
3 Aleph_w
4
5 Data structures & Algorithms
6 version 2.0.0b
7 https://github.com/lrleon/Aleph-w
8
9 This file is part of Aleph-w library
10
11 Copyright (c) 2002-2026 Leandro Rabindranath Leon
12
13 Permission is hereby granted, free of charge, to any person obtaining a copy
14 of this software and associated documentation files (the "Software"), to deal
15 in the Software without restriction, including without limitation the rights
16 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
17 copies of the Software, and to permit persons to whom the Software is
18 furnished to do so, subject to the following conditions:
19
20 The above copyright notice and this permission notice shall be included in all
21 copies or substantial portions of the Software.
22
23 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
28 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29 SOFTWARE.
30*/
31
32
68# ifndef PRIMES_H
69# define PRIMES_H
70
71# include <cmath>
72# include <ahDefs.H>
73# include <ah-errors.H>
74
75namespace Primes
76{
77
79 extern const unsigned long DefaultPrime;
80
82 extern const size_t numPrimes;
83
85 extern const unsigned long primeList[];
86
105 extern unsigned long next_prime(unsigned long n);
106
116 extern bool check_primes_database();
117
136 inline bool is_prime(unsigned long n)
137 {
138 if (n <= 1)
139 return false;
140
141 // i <= n / i is i * i <= n without overflow. i goes up to sqrt(n),
142 // which for a 64-bit n does not fit in an int
143 for (unsigned long i = 2; i <= n / i; i++)
144 if (n % i == 0)
145 return false;
146
147 return true;
148 }
149
171 inline unsigned long next_prime_number_greater_than(unsigned long n)
172 {
173 if (n <= 1)
174 return 2;
175
176 if (n == 2)
177 return 3;
178
179 const unsigned long max = std::numeric_limits<unsigned long>::max();
180
181 // max - 1 is even and max (2^32 - 1 or 2^64 - 1) is not prime, so no
182 // prime greater than max - 2 fits
183 ah_domain_error_if(n >= max - 2)
184 << "next_prime_number_greater_than: overflow error";
185
186 // the first odd number greater than n
187 unsigned long p = n % 2 == 0 ? n + 1 : n + 2;
188 while (not is_prime(p))
189 {
190 ah_domain_error_if(p + 1 == max or p + 2 == max)
191 << "next_prime_number_greater_than: overflow error";
192 p += 2;
193 }
194
195 return p;
196 }
197
198}; /* namespace Primes */
199
200# endif /* PRIMES_H */
Exception handling system with formatted messages for Aleph-w.
#define ah_domain_error_if(C)
Throws std::domain_error if condition holds.
Definition ah-errors.H:670
Core definitions, constants, and utility macros for Aleph-w.
__gmp_expr< typename __gmp_resolve_expr< T, V >::value_type, __gmp_binary_expr< __gmp_expr< T, U >, __gmp_expr< V, W >, __gmp_max_function > > max(const __gmp_expr< T, U > &expr1, const __gmp_expr< V, W > &expr2)
Definition gmpfrxx.h:4188
unsigned long next_prime_number_greater_than(unsigned long n)
Find the next prime strictly greater than n.
Definition primes.H:171
const unsigned long primeList[]
Pre-computed array of prime numbers for fast lookup.
Definition primes.C:45
const size_t numPrimes
Number of primes in the pre-computed database.
Definition primes.C:379
const unsigned long DefaultPrime
Default prime number used when no specific size is requested.
Definition primes.C:381
bool check_primes_database()
Verify the order of the prime database.
Definition primes.C:414
bool is_prime(unsigned long n)
Test if a number is prime using trial division.
Definition primes.H:136
unsigned long next_prime(unsigned long n)
Find a prime >= n in the database, for hash table sizing.
Definition primes.C:383