Aleph-w 3.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
98 extern unsigned long next_prime(unsigned long n);
99
108 extern bool check_primes_database();
109
128 inline bool is_prime(unsigned long n)
129 {
130 if (n <= 1)
131 return false;
132
133 for (int i = 2; i <= sqrt(n); i++)
134 if (n % i == 0)
135 return false;
136
137 return true;
138 }
139
161 inline unsigned long next_prime_number_greater_than(unsigned long n)
162 {
163 if (n <= 1)
164 return 2;
165
166 if (n == 2)
167 return 3;
168
169 const unsigned long max = std::numeric_limits<unsigned long>::max();
170
172 << "next_prime_number_greater_than: overflow error";
173
174 // if n is even, add 1
175 if (n % 2 == 0)
176 ++n;
177
179 << "next_prime_number_greater_than: overflow error";
180
181 unsigned long p = n + 2;
182 while (not is_prime(p))
183 {
184 ah_domain_error_if(p + 1 == max or p + 2 == max)
185 << "next_prime_number_greater_than: overflow error";
186 p += 2;
187 }
188
189 return p;
190 }
191
192}; /* namespace Primes */
193
194# 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:522
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:4110
__gmp_expr< T, __gmp_unary_expr< __gmp_expr< T, U >, __gmp_sqrt_function > > sqrt(const __gmp_expr< T, U > &expr)
Definition gmpfrxx.h:4058
unsigned long next_prime_number_greater_than(unsigned long n)
Find the next prime strictly greater than n.
Definition primes.H:161
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 integrity of the prime database.
Definition primes.C:411
bool is_prime(unsigned long n)
Test if a number is prime using trial division.
Definition primes.H:128
size_t next_prime(unsigned long n)
Find the smallest prime number >= n from the database.
Definition primes.C:383