Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
primality.H
Go to the documentation of this file.
1/*
2 Aleph_w
3
4 Data structures & Algorithms
5 version 2.0.0b
6 https://github.com/lrleon/Aleph-w
7
8 This file is part of Aleph-w library
9
10 Copyright (c) 2002-2026 Leandro Rabindranath Leon
11
12 Permission is hereby granted, free of charge, to any person obtaining a copy
13 of this software and associated documentation files (the "Software"), to deal
14 in the Software without restriction, including without limitation the rights
15 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16 copies of the Software, and to permit persons to whom the Software is
17 furnished to do so, subject to the following conditions:
18
19 The above copyright notice and this permission notice shall be included in all
20 copies or substantial portions of the Software.
21
22 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28 SOFTWARE.
29*/
30
41# ifndef PRIMALITY_H
42# define PRIMALITY_H
43
44# include <cstdint>
45# include <initializer_list>
46# include <modular_arithmetic.H>
47
48namespace Aleph
49{
50
51namespace detail
52{
64 uint64_t d, int s) noexcept
65{
66 uint64_t x = mod_exp(a, d, n);
67 if (x == 1 or x == n - 1)
68 return false;
69 for (int r = 1; r < s; r++)
70 {
71 x = mod_mul(x, x, n);
72 if (x == n - 1)
73 return false;
74 }
75 return true;
76}
77} // namespace detail
78
88[[nodiscard]] inline bool miller_rabin(uint64_t n) noexcept
89{
90 if (n < 2) return false;
91 if (n == 2 or n == 3) return true;
92 if (n % 2 == 0) return false;
93
94 uint64_t d = n - 1;
95 int s = 0;
96 while ((d & 1) == 0)
97 {
98 d >>= 1;
99 s++;
100 }
101
102 // These bases are sufficient for deterministic testing up to 2^64.
103 // Using Jim Sinclair's minimal 7-witness set for 64-bit range.
104 for (uint64_t a : {2, 325, 9375, 28178, 450775, 9780504, 1795265022})
105 {
106 if (n <= a)
107 break;
108 if (detail::check_composite(n, a, d, s))
109 return false;
110 }
111 return true;
112}
113
114} // namespace Aleph
115
116# endif // PRIMALITY_H
size_t blossom_maximum_cardinality_matching(const GT &g, DynDlist< typename GT::Arc * > &matching, SA sa=SA())
Alias of compute_maximum_cardinality_general_matching().
Definition Blossom.H:466
Safe modular arithmetic, extended Euclidean algorithm, and Chinese Remainder Theorem.
bool check_composite(uint64_t n, uint64_t a, uint64_t d, int s) noexcept
Helper function for Miller-Rabin test.
Definition primality.H:63
Main namespace for Aleph-w library functions.
Definition ah-arena.H:89
bool miller_rabin(uint64_t n) noexcept
Miller-Rabin primality test for 64-bit integers.
Definition primality.H:88
uint64_t mod_exp(uint64_t base, uint64_t exp, const uint64_t m)
Modular exponentiation.
uint64_t mod_mul(uint64_t a, uint64_t b, uint64_t m)
Safe 64-bit modular multiplication.
gsl_rng * r