Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
math_nt_example.cc
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
36#include <iostream>
37#include <iomanip>
38#include <vector>
39#include <print_rule.H>
40#include <modular_arithmetic.H>
41#include <primality.H>
42#include <pollard_rho.H>
43#include <ntt.H>
45#include <modular_linalg.H>
46
47using namespace std;
48using namespace Aleph;
49
51{
52 cout << "[1] Modular Arithmetic (Safe 64-bit)\n";
53 print_rule();
54
55 uint64_t a = 3000000000000000000ULL;
56 uint64_t b = 2000000000000000000ULL;
57 uint64_t m = 4000000000000000000ULL;
58
59 cout << "Safe multiplication avoiding 64-bit overflow:\n";
60 cout << " " << a << " * " << b << " mod " << m << "\n";
61 cout << " = " << mod_mul(a, b, m) << "\n\n";
62
63 cout << "Extended Euclidean Algorithm:\n";
64 int64_t x, y;
65 int64_t gcd = ext_gcd<int64_t>(30, 20, x, y);
66 cout << " gcd(30, 20) = " << gcd << "\n";
67 cout << " Coefficients: x=" << x << ", y=" << y << " (30*x + 20*y = " << (30*x + 20*y) << ")\n\n";
68
69 cout << "Chinese Remainder Theorem:\n";
70 Array<uint64_t> rem = {2, 3, 2};
71 Array<uint64_t> mod = {3, 5, 7};
72 cout << " x = 2 mod 3\n x = 3 mod 5\n x = 2 mod 7\n";
73 cout << " Solution: " << crt(rem, mod) << " (mod " << (3*5*7) << ")\n";
74 print_rule();
75 cout << "\n";
76}
77
79{
80 cout << "[2] Primality Testing & Factorization\n";
81 print_rule();
82
83 uint64_t prime_candidate = 2147483647; // 2^31 - 1
84 cout << "Miller-Rabin (deterministic for 64-bit):\n";
85 cout << " Is " << prime_candidate << " prime? "
86 << (miller_rabin(prime_candidate) ? "Yes" : "No") << "\n\n";
87
88 uint64_t composite = 1000000007ULL * 97ULL;
89 cout << "Pollard's Rho Factorization:\n";
90 cout << " Factoring " << composite << "...\n";
92 cout << " Prime factors: ";
93 for (size_t i = 0; i < factors.size(); ++i)
94 cout << factors[i] << (i + 1 == factors.size() ? "" : ", ");
95 cout << "\n";
96 print_rule();
97 cout << "\n";
98}
99
101{
102 cout << "[3] Number Theoretic Transform (NTT)\n";
103 print_rule();
104
105 cout << "Polynomial Multiplication modulo 998244353:\n";
106 Array<uint64_t> A = {1, 2}; // 1 + 2x
107 Array<uint64_t> B = {3, 4}; // 3 + 4x
108
109 cout << " A(x) = 1 + 2x\n";
110 cout << " B(x) = 3 + 4x\n";
111
112 auto C = NTT<>::multiply(A, B);
113 cout << " C(x) = A(x) * B(x) = ";
114 for (size_t i = 0; i < C.size(); ++i)
115 {
116 if (i > 0) cout << " + ";
117 cout << C[i];
118 if (i > 0) cout << "x^" << i;
119 }
120 cout << "\n";
121 print_rule();
122 cout << "\n";
123}
124
126{
127 cout << "[4] Modular Combinatorics & Lucas Theorem\n";
128 print_rule();
129
130 uint64_t mod = 1000000007;
131 ModularCombinatorics mc(10, mod);
132 cout << "O(1) Combinations (precomputed factorials):\n";
133 cout << " C(5, 2) mod " << mod << " = " << mc.nCk(5, 2) << "\n\n";
134
137 cout << "Lucas Theorem (large n, small prime):\n";
138 cout << " C(5, 2) mod " << small_mod << " = " << mc_lucas.lucas_nCk(5, 2) << "\n";
139 print_rule();
140 cout << "\n";
141}
142
144{
145 cout << "[5] Modular Linear Algebra (Gauss)\n";
146 print_rule();
147
150 m_data.append(Array<uint64_t>({3, 4}));
151
152 uint64_t p = 5;
153 ModularMatrix mat(m_data, p);
154
155 cout << "Matrix A (mod " << p << "):\n";
156 for (size_t i = 0; i < mat.get().size(); ++i)
157 cout << " [" << mat.get()[i][0] << ", " << mat.get()[i][1] << "]\n";
158
159 cout << "Determinant |A| = " << mat.determinant() << "\n";
160
161 auto inv_opt = mat.inverse();
162 if (inv_opt.has_value())
163 {
164 cout << "Inverse A^-1 (mod " << p << "):\n";
165 for (size_t i = 0; i < inv_opt->get().size(); ++i)
166 cout << " [" << inv_opt->get()[i][0] << ", " << inv_opt->get()[i][1] << "]\n";
167 }
168 else
169 {
170 cout << "Matrix is singular.\n";
171 }
172
173 cout << "\nSparse Matrix with DynMatrix (mod 7):\n";
175 sparse_data.write(0, 0, 2);
176 sparse_data.write(1, 1, 3);
177 sparse_data.write(2, 2, 4);
179
180 cout << "Diagonal Matrix D:\n";
181 for (size_t i = 0; i < 3; ++i)
182 cout << " [" << smat.get().read(i, 0) << ", " << smat.get().read(i, 1) << ", " << smat.get().read(i, 2) << "]\n";
183 cout << "Determinant |D| = " << smat.determinant() << "\n";
184
185 print_rule();
186 cout << "\n";
187}
188
189int main()
190{
191 cout << "\n=== Mathematical & Number Theory Toolkit ===\n\n";
192
195 demo_ntt();
198
199 cout << "Done.\n";
200 return 0;
201}
Simple dynamic array with automatic resizing and functional operations.
Definition tpl_array.H:139
T & append(const T &data)
Append a copy of data
Definition tpl_array.H:245
Dynamic matrix with sparse storage.
Definition tpl_dynMat.H:120
Combinatorics operations modulo a prime.
Matrix operations modulo a prime.
const MatrixT & get() const noexcept
Get a constant reference to the underlying matrix.
uint64_t determinant() const
Computes the determinant of the matrix modulo p.
std::optional< Modular_Matrix< MatrixT > > inverse() const
Computes the inverse of the matrix modulo p.
static Array< uint64_t > multiply(const Array< uint64_t > &a, const Array< uint64_t > &b)
Multiply two polynomials modulo MOD.
Definition ntt.H:1729
void demo_modular_linalg()
void demo_modular_arithmetic()
void demo_primality_factorization()
void demo_ntt()
void demo_modular_combinatorics()
int main()
Safe modular arithmetic, extended Euclidean algorithm, and Chinese Remainder Theorem.
Modular combinatorics utilities.
Linear algebra operations over finite fields (modulo a prime).
static mpfr_t y
Definition mpfr_mul_d.c:3
Main namespace for Aleph-w library functions.
Definition ah-arena.H:89
Array< uint64_t > pollard_rho(uint64_t n)
Compute the prime factorization of a number using Pollard's rho.
size_t size(Node *root) noexcept
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_mul(uint64_t a, uint64_t b, uint64_t m)
Safe 64-bit modular multiplication.
void print_rule()
Prints a horizontal rule for example output separation.
Definition print_rule.H:39
uint64_t crt(const Array< uint64_t > &rem, const Array< uint64_t > &mod)
Chinese Remainder Theorem (CRT).
STL namespace.
Industrial-grade Number Theoretic Transform core for modular polynomial multiplication.
Integer factorization using Pollard's rho algorithm.
Advanced primality testing algorithms.
FooMap m(5, fst_unit_pair_hash, snd_unit_pair_hash)