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
40# ifndef PRIMALITY_H
41# define PRIMALITY_H
42
43# include <cstdint>
44# include <initializer_list>
45# include <modular_arithmetic.H>
46
47namespace Aleph
48{
49
50namespace detail
51{
63 uint64_t d, int s) noexcept
64{
65 uint64_t x = mod_exp(a, d, n);
66 if (x == 1 or x == n - 1)
67 return false;
68 for (int r = 1; r < s; r++)
69 {
70 x = mod_mul(x, x, n);
71 if (x == n - 1)
72 return false;
73 }
74 return true;
75}
76} // namespace detail
77
87[[nodiscard]] inline bool miller_rabin(uint64_t n) noexcept
88{
89 if (n < 2) return false;
90 if (n == 2 or n == 3) return true;
91 if (n % 2 == 0) return false;
92
93 uint64_t d = n - 1;
94 int s = 0;
95 while ((d & 1) == 0)
96 {
97 d >>= 1;
98 s++;
99 }
100
101 // These bases are sufficient for deterministic testing up to 2^64.
102 // Using Jim Sinclair's minimal 7-witness set for 64-bit range.
103 for (uint64_t a : {2, 325, 9375, 28178, 450775, 9780504, 1795265022})
104 {
105 if (n <= a)
106 break;
107 if (detail::check_composite(n, a, d, s))
108 return false;
109 }
110 return true;
111}
112
113} // namespace Aleph
114
115# endif // PRIMALITY_H
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:62
Main namespace for Aleph-w library functions.
Definition ah-arena.H:89
Divide_Conquer_DP_Result< Cost > divide_and_conquer_partition_dp(const size_t groups, const size_t n, Transition_Cost_Fn transition_cost, const Cost inf=dp_optimization_detail::default_inf< Cost >())
Optimize partition DP using divide-and-conquer optimization.
bool miller_rabin(uint64_t n) noexcept
Miller-Rabin primality test for 64-bit integers.
Definition primality.H:87
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