Skip to content

Commit bf6b463

Browse files
committed
refactor: rename modint.hpp to mod_utility.hpp
1 parent 22745c0 commit bf6b463

File tree

3 files changed

+31
-10
lines changed

3 files changed

+31
-10
lines changed

test/primality_test.test.cpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,7 @@ int main() {
1414
while (q--) {
1515
uint64_t n;
1616
cin >> n;
17-
if (n <= UINT32_MAX)
18-
cout << (miller_rabin32(n) ? "Yes\n" : "No\n");
19-
else
20-
cout << (miller_rabin64(n) ? "Yes\n" : "No\n");
17+
cout << (is_prime(n) ? "Yes\n" : "No\n");
2118
}
2219
return 0;
2320
}

weilycoder/number-theory/modint.hpp renamed to weilycoder/number-theory/mod_utility.hpp

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
#ifndef WEILYCODER_MODINT_HPP
2-
#define WEILYCODER_MODINT_HPP
1+
#ifndef WEILYCODER_MOD_UTILITY_HPP
2+
#define WEILYCODER_MOD_UTILITY_HPP
33

44
/**
5-
* @file modint.hpp
6-
* @brief Modular Integer Arithmetic Utilities
5+
* @file mod_utility.hpp
6+
* @brief Modular Arithmetic Utilities
77
*/
88

99
#include <cstdint>
@@ -26,6 +26,24 @@ uint64_t modular_multiply_64(uint64_t a, uint64_t b, uint64_t modulus) {
2626
return static_cast<unsigned __int128>(a) * b % modulus;
2727
}
2828

29+
/**
30+
* @brief Perform modular multiplication for 64-bit integers with a compile-time
31+
* modulus.
32+
* @tparam Modulus The modulus.
33+
* @tparam bit32 If true, won't use 128-bit arithmetic. You should ensure that
34+
* all inputs are small enough to avoid overflow (i.e. bit-32).
35+
* @param a The first multiplicand.
36+
* @param b The second multiplicand.
37+
* @return (a * b) % Modulus
38+
*/
39+
template <uint64_t Modulus, bool bit32 = false>
40+
uint64_t modular_multiply_64(uint64_t a, uint64_t b) {
41+
if constexpr (bit32)
42+
return a * b % Modulus;
43+
else
44+
return static_cast<unsigned __int128>(a) * b % Modulus;
45+
}
46+
2947
/**
3048
* @brief Perform modular exponentiation for 64-bit integers.
3149
* @tparam bit32 If true, won't use 128-bit arithmetic. You should ensure that
@@ -47,6 +65,6 @@ constexpr uint64_t fast_power_64(uint64_t base, uint64_t exponent, uint64_t modu
4765
}
4866
return result;
4967
}
50-
} // namespace weilycoder
68+
}
5169

5270
#endif

weilycoder/number-theory/prime.hpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* @brief Prime Number Utilities
77
*/
88

9-
#include "modint.hpp"
9+
#include "mod_utility.hpp"
1010
#include <cstdint>
1111
#include <type_traits>
1212

@@ -94,6 +94,12 @@ constexpr bool miller_rabin64(uint64_t n) {
9494
* @return true if n is prime, false if not prime.
9595
*/
9696
constexpr bool miller_rabin32(uint32_t n) { return miller_rabin<true, 2, 7, 61>(n); }
97+
98+
constexpr bool is_prime(uint64_t n) {
99+
if (n <= UINT32_MAX)
100+
return miller_rabin32(static_cast<uint32_t>(n));
101+
return miller_rabin64(n);
102+
}
97103
} // namespace weilycoder
98104

99105
#endif

0 commit comments

Comments
 (0)