12namespace layer1_foundations {
13namespace number_theory {
20static int the_first_thousand_primes[] = {
21 2, 3, 5, 7, 11, 13, 17, 19, 23, 29
22, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71
23, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113
24, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173
25, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229
26, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281
27, 283, 293, 307, 311, 313, 317, 331, 337, 347, 349
28, 353, 359, 367, 373, 379, 383, 389, 397, 401, 409
29, 419, 421, 431, 433, 439, 443, 449, 457, 461, 463
30, 467, 479, 487, 491, 499, 503, 509, 521, 523, 541
31, 547, 557, 563, 569, 571, 577, 587, 593, 599, 601
32, 607, 613, 617, 619, 631, 641, 643, 647, 653, 659
33, 661, 673, 677, 683, 691, 701, 709, 719, 727, 733
34, 739, 743, 751, 757, 761, 769, 773, 787, 797, 809
35, 811, 821, 823, 827, 829, 839, 853, 857, 859, 863
36, 877, 881, 883, 887, 907, 911, 919, 929, 937, 941
37, 947, 953, 967, 971, 977, 983, 991, 997, 1009, 1013
38, 1019, 1021, 1031, 1033, 1039, 1049, 1051, 1061, 1063, 1069
39, 1087, 1091, 1093, 1097, 1103, 1109, 1117, 1123, 1129, 1151
40, 1153, 1163, 1171, 1181, 1187, 1193, 1201, 1213, 1217, 1223
41, 1229, 1231, 1237, 1249, 1259, 1277, 1279, 1283, 1289, 1291
42, 1297, 1301, 1303, 1307, 1319, 1321, 1327, 1361, 1367, 1373
43, 1381, 1399, 1409, 1423, 1427, 1429, 1433, 1439, 1447, 1451
44, 1453, 1459, 1471, 1481, 1483, 1487, 1489, 1493, 1499, 1511
45, 1523, 1531, 1543, 1549, 1553, 1559, 1567, 1571, 1579, 1583
46, 1597, 1601, 1607, 1609, 1613, 1619, 1621, 1627, 1637, 1657
47, 1663, 1667, 1669, 1693, 1697, 1699, 1709, 1721, 1723, 1733
48, 1741, 1747, 1753, 1759, 1777, 1783, 1787, 1789, 1801, 1811
49, 1823, 1831, 1847, 1861, 1867, 1871, 1873, 1877, 1879, 1889
50, 1901, 1907, 1913, 1931, 1933, 1949, 1951, 1973, 1979, 1987
51, 1993, 1997, 1999, 2003, 2011, 2017, 2027, 2029, 2039, 2053
52, 2063, 2069, 2081, 2083, 2087, 2089, 2099, 2111, 2113, 2129
53, 2131, 2137, 2141, 2143, 2153, 2161, 2179, 2203, 2207, 2213
54, 2221, 2237, 2239, 2243, 2251, 2267, 2269, 2273, 2281, 2287
55, 2293, 2297, 2309, 2311, 2333, 2339, 2341, 2347, 2351, 2357
56, 2371, 2377, 2381, 2383, 2389, 2393, 2399, 2411, 2417, 2423
57, 2437, 2441, 2447, 2459, 2467, 2473, 2477, 2503, 2521, 2531
58, 2539, 2543, 2549, 2551, 2557, 2579, 2591, 2593, 2609, 2617
59, 2621, 2633, 2647, 2657, 2659, 2663, 2671, 2677, 2683, 2687
60, 2689, 2693, 2699, 2707, 2711, 2713, 2719, 2729, 2731, 2741
61, 2749, 2753, 2767, 2777, 2789, 2791, 2797, 2801, 2803, 2819
62, 2833, 2837, 2843, 2851, 2857, 2861, 2879, 2887, 2897, 2903
63, 2909, 2917, 2927, 2939, 2953, 2957, 2963, 2969, 2971, 2999
64, 3001, 3011, 3019, 3023, 3037, 3041, 3049, 3061, 3067, 3079
65, 3083, 3089, 3109, 3119, 3121, 3137, 3163, 3167, 3169, 3181
66, 3187, 3191, 3203, 3209, 3217, 3221, 3229, 3251, 3253, 3257
67, 3259, 3271, 3299, 3301, 3307, 3313, 3319, 3323, 3329, 3331
68, 3343, 3347, 3359, 3361, 3371, 3373, 3389, 3391, 3407, 3413
69, 3433, 3449, 3457, 3461, 3463, 3467, 3469, 3491, 3499, 3511
70, 3517, 3527, 3529, 3533, 3539, 3541, 3547, 3557, 3559, 3571
71, 3581, 3583, 3593, 3607, 3613, 3617, 3623, 3631, 3637, 3643
72, 3659, 3671, 3673, 3677, 3691, 3697, 3701, 3709, 3719, 3727
73, 3733, 3739, 3761, 3767, 3769, 3779, 3793, 3797, 3803, 3821
74, 3823, 3833, 3847, 3851, 3853, 3863, 3877, 3881, 3889, 3907
75, 3911, 3917, 3919, 3923, 3929, 3931, 3943, 3947, 3967, 3989
76, 4001, 4003, 4007, 4013, 4019, 4021, 4027, 4049, 4051, 4057
77, 4073, 4079, 4091, 4093, 4099, 4111, 4127, 4129, 4133, 4139
78, 4153, 4157, 4159, 4177, 4201, 4211, 4217, 4219, 4229, 4231
79, 4241, 4243, 4253, 4259, 4261, 4271, 4273, 4283, 4289, 4297
80, 4327, 4337, 4339, 4349, 4357, 4363, 4373, 4391, 4397, 4409
81, 4421, 4423, 4441, 4447, 4451, 4457, 4463, 4481, 4483, 4493
82, 4507, 4513, 4517, 4519, 4523, 4547, 4549, 4561, 4567, 4583
83, 4591, 4597, 4603, 4621, 4637, 4639, 4643, 4649, 4651, 4657
84, 4663, 4673, 4679, 4691, 4703, 4721, 4723, 4729, 4733, 4751
85, 4759, 4783, 4787, 4789, 4793, 4799, 4801, 4813, 4817, 4831
86, 4861, 4871, 4877, 4889, 4903, 4909, 4919, 4931, 4933, 4937
87, 4943, 4951, 4957, 4967, 4969, 4973, 4987, 4993, 4999, 5003
88, 5009, 5011, 5021, 5023, 5039, 5051, 5059, 5077, 5081, 5087
89, 5099, 5101, 5107, 5113, 5119, 5147, 5153, 5167, 5171, 5179
90, 5189, 5197, 5209, 5227, 5231, 5233, 5237, 5261, 5273, 5279
91, 5281, 5297, 5303, 5309, 5323, 5333, 5347, 5351, 5381, 5387
92, 5393, 5399, 5407, 5413, 5417, 5419, 5431, 5437, 5441, 5443
93, 5449, 5471, 5477, 5479, 5483, 5501, 5503, 5507, 5519, 5521
94, 5527, 5531, 5557, 5563, 5569, 5573, 5581, 5591, 5623, 5639
95, 5641, 5647, 5651, 5653, 5657, 5659, 5669, 5683, 5689, 5693
96, 5701, 5711, 5717, 5737, 5741, 5743, 5749, 5779, 5783, 5791
97, 5801, 5807, 5813, 5821, 5827, 5839, 5843, 5849, 5851, 5857
98, 5861, 5867, 5869, 5879, 5881, 5897, 5903, 5923, 5927, 5939
99, 5953, 5981, 5987, 6007, 6011, 6029, 6037, 6043, 6047, 6053
100, 6067, 6073, 6079, 6089, 6091, 6101, 6113, 6121, 6131, 6133
101, 6143, 6151, 6163, 6173, 6197, 6199, 6203, 6211, 6217, 6221
102, 6229, 6247, 6257, 6263, 6269, 6271, 6277, 6287, 6299, 6301
103, 6311, 6317, 6323, 6329, 6337, 6343, 6353, 6359, 6361, 6367
104, 6373, 6379, 6389, 6397, 6421, 6427, 6449, 6451, 6469, 6473
105, 6481, 6491, 6521, 6529, 6547, 6551, 6553, 6563, 6569, 6571
106, 6577, 6581, 6599, 6607, 6619, 6637, 6653, 6659, 6661, 6673
107, 6679, 6689, 6691, 6701, 6703, 6709, 6719, 6733, 6737, 6761
108, 6763, 6779, 6781, 6791, 6793, 6803, 6823, 6827, 6829, 6833
109, 6841, 6857, 6863, 6869, 6871, 6883, 6899, 6907, 6911, 6917
110, 6947, 6949, 6959, 6961, 6967, 6971, 6977, 6983, 6991, 6997
111, 7001, 7013, 7019, 7027, 7039, 7043, 7057, 7069, 7079, 7103
112, 7109, 7121, 7127, 7129, 7151, 7159, 7177, 7187, 7193, 7207
113, 7211, 7213, 7219, 7229, 7237, 7243, 7247, 7253, 7283, 7297
114, 7307, 7309, 7321, 7331, 7333, 7349, 7351, 7369, 7393, 7411
115, 7417, 7433, 7451, 7457, 7459, 7477, 7481, 7487, 7489, 7499
116, 7507, 7517, 7523, 7529, 7537, 7541, 7547, 7549, 7559, 7561
117, 7573, 7577, 7583, 7589, 7591, 7603, 7607, 7621, 7639, 7643
118, 7649, 7669, 7673, 7681, 7687, 7691, 7699, 7703, 7717, 7723
119, 7727, 7741, 7753, 7757, 7759, 7789, 7793, 7817, 7823, 7829
120, 7841, 7853, 7867, 7873, 7877, 7879, 7883, 7901, 7907, 7919
136 int f_negative =
FALSE;
144 if (f_negative && r) {
169 A.
create(a, __FILE__, __LINE__);
170 N.
create(n, __FILE__, __LINE__);
171 M.
create(p, __FILE__, __LINE__);
182 A.
create(a, __FILE__, __LINE__);
183 B.
create(p, __FILE__, __LINE__);
186 cout <<
"number_theory_domain::inverse_mod a and p are not coprime" << endl;
187 cout <<
"a=" << a << endl;
188 cout <<
"p=" << p << endl;
189 cout <<
"gcd=" << G << endl;
204 A.
create(a, __FILE__, __LINE__);
205 B.
create(b, __FILE__, __LINE__);
206 P.
create(p, __FILE__, __LINE__);
217 A.
create(a, __FILE__, __LINE__);
218 B.
create(b, __FILE__, __LINE__);
219 P.
create(p, __FILE__, __LINE__);
231 A.
create(a, __FILE__, __LINE__);
232 B.
create(b, __FILE__, __LINE__);
251 longinteger_domain D;
252 longinteger_object M, N, G, U, V;
257 D.extended_gcd(M, N, G, U, V, 0);
294 M.
create(m, __FILE__, __LINE__);
295 N.
create(n, __FILE__, __LINE__);
303 long int &g,
long int &u,
long int &v)
309 M.
create(m, __FILE__, __LINE__);
310 N.
create(n, __FILE__, __LINE__);
318 long int a,
long int b,
int f_key,
int verbose_level)
321 int f_v = (verbose_level >= 1);
322 long int a1, b1, q1, r1;
325 cout <<
"number_theory_domain::gcd_with_key_in_latex "
326 "a=" << a <<
", b=" << b <<
":" << endl;
344 ost <<
" \\gcd\\big( " << b1 <<
", " << r1 <<
"\\big) "
345 "\\qquad \\mbox{b/c} \\; " << a1 <<
" = " << q1
346 <<
" \\cdot " << b1 <<
" + " << r1 <<
"\\\\" << endl;
349 cout <<
"number_theory_domain::gcd_with_key_in_latex "
350 "a1=" << a1 <<
" b1=" << b1
351 <<
" r1=" << r1 <<
" q1=" << q1
361 ost <<
"= " << b1 <<
"\\\\" << endl;
364 cout <<
"number_theory_domain::gcd_with_key_in_latex done" << endl;
376 a.
create(i, __FILE__, __LINE__);
379 b.
create(res, __FILE__, __LINE__);
383 cout <<
"i_power_j_safe int overflow when computing "
384 << i <<
"^" << j << endl;
385 cout <<
"should be " << a << endl;
386 cout <<
"but comes out as " << res << endl;
394 int f_v = (verbose_level >= 1);
401 cout <<
"number_theory_domain::i_power_j_lint_safe "
402 "i=" << i <<
" j=" << j << endl;
404 a.
create(i, __FILE__, __LINE__);
407 cout <<
"number_theory_domain::i_power_j_lint_safe "
412 cout <<
"number_theory_domain::i_power_j_lint_safe "
413 "as_lint=" << res << endl;
415 b.
create(res, __FILE__, __LINE__);
417 cout <<
"number_theory_domain::i_power_j_lint_safe "
423 cout <<
"number_theory_domain::i_power_j_lint_safe "
427 cout <<
"i_power_j_safe int overflow when computing "
428 << i <<
"^" << j << endl;
429 cout <<
"should be " << a << endl;
430 cout <<
"but comes out as " << res << endl;
443 for (k = 0; k < j; k++) {
457 for (k = 0; k < j; k++) {
467 int f_v = (verbose_level >= 1);
477 cout <<
"number_theory_domain::do_eulerfunction_interval n_min=" << n_min <<
" n_max=" << n_max << endl;
480 std::vector<std::pair<long int, long int>> Table;
482 for (n = n_min; n <= n_max; n++) {
485 std::pair<long int, long int> P;
489 cout <<
"eulerfunction of " << n <<
" is " << a << endl;
499 for (i = 0; i < Table.size(); i++) {
500 T[2 * i + 0] = Table[i].first;
501 T[2 * i + 1] = Table[i].second;
503 sprintf(str,
"table_eulerfunction_%ld_%ld.csv", n_min, n_max);
510 Headers =
new string[2];
511 Headers[0].assign(
"N");
512 Headers[1].assign(
"PHI");
516 cout <<
"Written file " << fname <<
" of size " << Fio.
file_size(fname) << endl;
526 cout <<
"number_theory_domain::do_eulerfunction_interval done" << endl;
540 long int i, k, p1, e1;
545 for (i = 0; i < len; i++) {
569 for (i = 0; i < len; i++) {
570 if (exponents[i] > 1) {
593 cout <<
"number_theory_domain::order_mod_p a < 0" << endl;
618 cout <<
"int_log2 n <= 0" << endl;
621 for (i = 0; n > 0; i++) {
633 cout <<
"int_log10 n <= 0" << endl;
634 cout <<
"n = " << n << endl;
651 cout <<
"lint_log10 n <= 0" << endl;
652 cout <<
"n = " << n << endl;
668 cout <<
"int_logq n < 0" << endl;
685 cout <<
"int_logq n < 0" << endl;
814 if (
EVEN((p_min - 1) >> 1))
821 cout <<
"n=" << n <<
" i=" << i <<
" q=" << q << endl;
840 int alloc_len = 10, len = 0;
844 exponents =
NEW_int(alloc_len);
847 cout <<
"factor_int, the number is one" << endl;
851 cout <<
"factor_int, the number is <= 0" << endl;
863 if (p == primes[len - 1]) {
864 exponents[len - 1]++;
867 if (len == alloc_len) {
868 int *primes2, *exponents2;
872 exponents2 =
NEW_int(alloc_len);
873 for (i = 0; i < len; i++) {
874 primes2[i] = primes[i];
875 exponents2[i] = exponents[i];
880 exponents = exponents2;
896 cout <<
"number_theory_domain::factor_lint, the number is one" << endl;
900 cout <<
"number_theory_domain::factor_lint, the number is <= 0" << endl;
908 exponents[exponents.size()]++;
912 exponents.push_back(1);
921 cout <<
"factor_prime_power q is one" << endl;
929 cout <<
"factor_prime_power q is not a prime power" << endl;
941 int f_v = (verbose_level >= 1);
942 long int i, pm1, a, n, b;
943 vector<long int> primes;
944 vector<int> exponents;
949 cout <<
"number_theory_domain::primitive_root_randomized p=" << p << endl;
952 cout <<
"number_theory_domain::primitive_root_randomized: p < 2" << endl;
958 cout <<
"number_theory_domain::primitive_root_randomized before factor_lint " << pm1 << endl;
962 cout <<
"number_theory_domain::primitive_root_randomized after factor_lint " << pm1 << endl;
963 cout <<
"number_theory_domain::primitive_root_randomized number of factors is " << primes.size() << endl;
972 cout <<
"number_theory_domain::primitive_root_randomized iteration " << cnt <<
" : trying " << a << endl;
974 for (i = 0; i < (
long int) primes.size(); i++) {
977 cout <<
"number_theory_domain::primitive_root_randomized iteration " << cnt <<
" : trying " << a
978 <<
" : prime factor " << i <<
" / " << primes.size() <<
" raising to the power " << n << endl;
982 cout <<
"number_theory_domain::primitive_root_randomized iteration " << cnt
983 <<
" : trying " << a <<
" : prime factor " << i <<
" / " << primes.size()
984 <<
" raising to the power " << n <<
" yields " << b << endl;
991 if (i == (
long int)primes.size()) {
997 cout <<
"number_theory_domain::primitive_root_randomized done after " << cnt <<
" trials" << endl;
1006 int f_v = (verbose_level >= 1);
1010 cout <<
"primitive_root: p < 2" << endl;
1016 for (i = 2; i < p; i++) {
1020 cout << i <<
" is primitive root mod " << p << endl;
1025 cout <<
"no primitive root found" << endl;
1032 return Jacobi(a, p, verbose_level);
1038 int f_v = (verbose_level >= 1);
1039 long int a1, m1, ord2, r1;
1041 int f_negative =
FALSE;
1045 cout <<
"Jacobi(" << a <<
", " << m <<
")" << endl;
1060 cout <<
"Jacobi a1 == 0" << endl;
1065 cout <<
"Jacobi = " << r1
1066 <<
" * Jacobi(" << a1 <<
", " << m1 <<
")" << endl;
1090 if (m1 % 8 == 3 || m1 % 8 == 5)
1098 if ((t1 % 2) && (t2 % 2))
1104 cout <<
"Jacobi = " << r1
1105 <<
" * Jacobi(" << a1 <<
", " << m1 <<
")" << endl;
1112 cout <<
"Jacobi a1 == -1 || a1 == 0" << endl;
1115 cout <<
"Jacobi wrong termination" << endl;
1120 long int a,
long int m,
int verbose_level)
1123 int f_v = (verbose_level >= 1);
1124 long int a1, m1, ord2, r1;
1126 int f_negative =
FALSE;
1130 cout <<
"number_theory_domain::Jacobi_with_key_in_latex(" << a <<
", " << m <<
")" << endl;
1134 ost <<
"$\\Big( \\frac{" << a <<
" }{ "
1135 << m <<
"}\\Big)$\\\\" << endl;
1151 cout <<
"number_theory_domain::Jacobi_with_key_in_latex a1 == 0" << endl;
1158 ost <<
"(-1) \\cdot ";
1160 ost <<
" \\Big( \\frac{" << a1 % m1 <<
" }{ "
1161 << m1 <<
"}\\Big)$\\\\" << endl;
1168 cout <<
"Jacobi = " << r1 <<
" * Jacobi("
1169 << a1 <<
", " << m1 <<
")" << endl;
1178 ost <<
"(-1) \\cdot ";
1180 ost <<
"\\Big( \\frac{-1 }{ " << m1
1181 <<
"}\\Big) \\cdot \\Big( \\frac{"
1183 << m1 <<
"}\\Big)$\\\\" << endl;
1186 ost <<
"(-1) \\cdot ";
1188 ost <<
"(-1)^{\\frac{" << m1
1189 <<
"-1}{2}} \\cdot \\Big( \\frac{"
1191 << m1 <<
"}\\Big)$\\\\" << endl;
1208 ost <<
"(-1) \\cdot ";
1210 ost <<
" \\Big( \\frac{"
1212 <<
" }{ " << m1 <<
"}\\Big)$\\\\" << endl;
1224 ost <<
"(-1) \\cdot ";
1226 ost <<
"\\Big( \\frac{2}{ " << m1
1227 <<
"}\\Big)^{" << ord2
1228 <<
"} \\cdot \\Big( \\frac{" << a1
1229 <<
" }{ " << m1 <<
"}\\Big)$\\\\" << endl;
1232 ost <<
"(-1) \\cdot ";
1234 ost <<
"\\Big( (-1)^{\\frac{" << m1
1235 <<
"^2-1}{8}} \\Big)^{" << ord2
1236 <<
"} \\cdot \\Big( \\frac{" << a1 <<
" }{ "
1237 << m1 <<
"}\\Big)$\\\\" << endl;
1242 ost <<
"(-1) \\cdot ";
1244 ost <<
"\\Big( \\frac{2}{ " << m1
1245 <<
"}\\Big) \\cdot \\Big( \\frac{" << a1
1246 <<
" }{ " << m1 <<
"}\\Big)$\\\\" << endl;
1249 ost <<
"(-1) \\cdot ";
1251 ost <<
"(-1)^{\\frac{" << m1
1252 <<
"^2-1}{8}} \\cdot \\Big( \\frac{" << a1
1253 <<
" }{ " << m1 <<
"}\\Big)$\\\\" << endl;
1256 if (m1 % 8 == 3 || m1 % 8 == 5) {
1262 ost <<
"(-1) \\cdot ";
1264 ost <<
"\\Big( \\frac{" << a1 <<
" }{ " << m1
1265 <<
"}\\Big)$\\\\" << endl;
1273 ost <<
"(-1) \\cdot ";
1275 ost <<
"\\Big( \\frac{2}{ " << m1 <<
"}\\Big)^{"
1276 << ord2 <<
"} \\cdot \\Big( \\frac{" << a1
1277 <<
" }{ " << m1 <<
"}\\Big)$\\\\" << endl;
1281 ost <<
"(-1) \\cdot ";
1283 ost <<
"\\Big( (-1)^{\\frac{" << m1
1284 <<
"^2-1}{8}} \\Big)^{" << ord2
1285 <<
"} \\cdot \\Big( \\frac{" << a1 <<
" }{ "
1286 << m1 <<
"}\\Big)$\\\\" << endl;
1289 ost <<
"(-1) \\cdot ";
1291 ost <<
"\\Big( \\frac{" << a1 <<
" }{ " << m1
1292 <<
"}\\Big)$\\\\" << endl;
1307 ost <<
"(-1) \\cdot ";
1309 ost <<
"\\Big( \\frac{" << a1 <<
" }{ " << m1
1310 <<
"}\\Big) \\cdot (-1)^{\\frac{" << m1
1311 <<
"-1}{2} \\cdot \\frac{" << a1
1312 <<
" - 1}{2}}$\\\\" << endl;
1320 if ((t1 % 2) && (t2 % 2)) {
1328 ost <<
"(-1) \\cdot ";
1330 ost <<
"\\Big( \\frac{" << a1 <<
" }{ " << m1
1331 <<
"}\\Big)$\\\\" << endl;
1334 cout <<
"number_theory_domain::Jacobi_with_key_in_latex = " << r1 <<
" * Jacobi(" << a1
1335 <<
", " << m1 <<
")" << endl;
1339 ost <<
"$=" << r1 <<
"$\\\\" << endl;
1343 cout <<
"number_theory_domain::Jacobi_with_key_in_latex a1 == -1 || a1 == 0" << endl;
1346 cout <<
"number_theory_domain::Jacobi_with_key_in_latex wrong termination" << endl;
1351 long int a,
long int m,
int verbose_level)
1354 int f_v = (verbose_level >= 1);
1355 long int a1, m1, ord2, r1;
1357 int f_negative =
FALSE;
1361 cout <<
"number_theory_domain::Legendre_with_key_in_latex(" << a <<
", " << m <<
")" << endl;
1365 ost <<
"$\\Big( \\frac{" << a <<
" }{ "
1366 << m <<
"}\\Big)$\\\\" << endl;
1382 cout <<
"number_theory_domain::Legendre_with_key_in_latex a1 == 0" << endl;
1389 ost <<
"(-1) \\cdot ";
1391 ost <<
" \\Big( \\frac{" << a1 % m1 <<
" }{ "
1392 << m1 <<
"}\\Big)$\\\\" << endl;
1399 cout <<
"Jacobi = " << r1 <<
" * Jacobi("
1400 << a1 <<
", " << m1 <<
")" << endl;
1409 ost <<
"(-1) \\cdot ";
1411 ost <<
"\\Big( \\frac{-1 }{ " << m1
1412 <<
"}\\Big) \\cdot \\Big( \\frac{"
1414 << m1 <<
"}\\Big)$\\\\" << endl;
1417 ost <<
"(-1) \\cdot ";
1419 ost <<
"(-1)^{\\frac{" << m1
1420 <<
"-1}{2}} \\cdot \\Big( \\frac{"
1422 << m1 <<
"}\\Big)$\\\\" << endl;
1436 ost <<
"(-1) \\cdot ";
1438 ost <<
" \\Big( \\frac{"
1440 <<
" }{ " << m1 <<
"}\\Big)$\\\\" << endl;
1452 ost <<
"(-1) \\cdot ";
1454 ost <<
"\\Big( \\frac{2}{ " << m1
1455 <<
"}\\Big)^{" << ord2
1456 <<
"} \\cdot \\Big( \\frac{" << a1
1457 <<
" }{ " << m1 <<
"}\\Big)$\\\\" << endl;
1460 ost <<
"(-1) \\cdot ";
1462 ost <<
"\\Big( (-1)^{\\frac{" << m1
1463 <<
"^2-1}{8}} \\Big)^{" << ord2
1464 <<
"} \\cdot \\Big( \\frac{" << a1 <<
" }{ "
1465 << m1 <<
"}\\Big)$\\\\" << endl;
1470 ost <<
"(-1) \\cdot ";
1472 ost <<
"\\Big( \\frac{2}{ " << m1
1473 <<
"}\\Big) \\cdot \\Big( \\frac{" << a1
1474 <<
" }{ " << m1 <<
"}\\Big)$\\\\" << endl;
1477 ost <<
"(-1) \\cdot ";
1479 ost <<
"(-1)^{\\frac{" << m1
1480 <<
"^2-1}{8}} \\cdot \\Big( \\frac{" << a1
1481 <<
" }{ " << m1 <<
"}\\Big)$\\\\" << endl;
1484 if (m1 % 8 == 3 || m1 % 8 == 5) {
1490 ost <<
"(-1) \\cdot ";
1492 ost <<
"\\Big( \\frac{" << a1 <<
" }{ " << m1
1493 <<
"}\\Big)$\\\\" << endl;
1501 ost <<
"(-1) \\cdot ";
1503 ost <<
"\\Big( \\frac{2}{ " << m1 <<
"}\\Big)^{"
1504 << ord2 <<
"} \\cdot \\Big( \\frac{" << a1
1505 <<
" }{ " << m1 <<
"}\\Big)$\\\\" << endl;
1509 ost <<
"(-1) \\cdot ";
1511 ost <<
"\\Big( (-1)^{\\frac{" << m1
1512 <<
"^2-1}{8}} \\Big)^{" << ord2
1513 <<
"} \\cdot \\Big( \\frac{" << a1 <<
" }{ "
1514 << m1 <<
"}\\Big)$\\\\" << endl;
1517 ost <<
"(-1) \\cdot ";
1519 ost <<
"\\Big( \\frac{" << a1 <<
" }{ " << m1
1520 <<
"}\\Big)$\\\\" << endl;
1535 ost <<
"(-1) \\cdot ";
1537 ost <<
"\\Big( \\frac{" << a1 <<
" }{ " << m1
1538 <<
"}\\Big) \\cdot (-1)^{\\frac{" << m1
1539 <<
"-1}{2} \\cdot \\frac{" << a1
1540 <<
" - 1}{2}}$\\\\" << endl;
1550 if ((t1 % 2) && (t2 % 2)) {
1556 ost <<
"(-1) \\cdot ";
1558 ost <<
"\\Big( \\frac{" << a1 <<
" }{ " << m1
1559 <<
"}\\Big)$\\\\" << endl;
1562 cout <<
"number_theory_domain::Jacobi = " << r1 <<
" * Jacobi(" << a1
1563 <<
", " << m1 <<
")" << endl;
1567 ost <<
"$=" << r1 <<
"$\\\\" << endl;
1571 cout <<
"number_theory_domain::Legendre_with_key_in_latex a1 == -1 || a1 == 0" << endl;
1574 cout <<
"number_theory_domain::Legendre_with_key_in_latex wrong termination" << endl;
1588 cout <<
"number_theory_domain::ny2 x == 0" << endl;
1619 cout <<
"number_theory_domain::ny_p n == 0" << endl;
1638long int number_theory_domain::sqrt_mod_simple(
long int a,
long int p)
1644 for (x = 0; x < p; x++) {
1645 if ((x * x) % p == a1) {
1649 cout <<
"number_theory_domain::sqrt_mod_simple the number a is "
1650 "not a quadratic residue mod p" << endl;
1651 cout <<
"a = " << a <<
" p=" << p << endl;
1661 if (exponents[i] > 1) {
1662 cout <<
"^" << exponents[i];
1677 if (exponents[i] > 1) {
1678 cout <<
"^" << exponents[i];
1688 int bt,
int bb,
int &ct,
int &cb,
1691 int f_v = (verbose_level >= 1);
1707 ct = at * b1 + bt * a1;
1719 cout <<
"int_add_fractions " << at <<
"/"
1720 << ab <<
" + " << bt <<
"/" << bb <<
" = "
1721 << ct <<
"/" << cb << endl;
1726 int bt,
int bb,
int &ct,
int &cb,
1741 if (g != 1 && g != -1) {
1746 if (g != 1 && g != -1) {
1751 if (g != 1 && g != -1) {
1756 if (g != 1 && g != -1) {
1778 int nb_primes =
sizeof(the_first_thousand_primes) /
sizeof(
int);
1783 p = the_first_thousand_primes[r];
1784 if (p > lower_bound) {
1793 int nb_primes =
sizeof(the_first_thousand_primes) /
sizeof(
int);
1798 p = the_first_thousand_primes[r];
1799 if (p > lower_bound && p < upper_bound) {
1812 if (r > lower_bound) {
1823 if (upper_bound <= lower_bound) {
1824 cout <<
"random_integer_in_interval upper_bound <= lower_bound" << endl;
1827 n = upper_bound - lower_bound;
1834 return sizeof(the_first_thousand_primes) /
sizeof(
int);
1839 return the_first_thousand_primes[idx];
1843 long int p1,
long int p2,
int verbose_level)
1845 long int k, ma1, p1v, x;
1856 long int p,
long int g,
long int h,
1857 int f_latex, ostream &ost,
int verbose_level)
1859 int f_v = (verbose_level >= 1);
1865 long int gn, gmn, hgmn;
1871 cout <<
"number_theory_domain::do_babystep_giantstep "
1872 "p=" << p <<
" g=" << g <<
" h=" << h << endl;
1876 cout <<
"a primitive root modulo " << p <<
" is " << r << endl;
1881 n = 1 + (int) sqrtN;
1883 cout <<
"do_babystep_giantstep "
1887 <<
" n=" << n << endl;
1894 cout <<
"g^n=" << gn << endl;
1898 cout <<
"g^-n=" << gmn << endl;
1902 cout <<
"h*g^-n=" << hgmn << endl;
1906 for (i = 1; i < n; i++) {
1907 Table1[i] = NT.
mult_mod(Table1[i - 1], g, p);
1908 Table2[i] = NT.
mult_mod(Table2[i - 1], gmn, p);
1914 cout <<
"duplicates:" << endl;
1915 for (i = 1; i < 2 * n; i++) {
1916 if (data[i] == data[i - 1]) {
1917 cout << data[i] << endl;
1923 ost <<
"$$" << endl;
1924 ost <<
"\\begin{array}[t]{|r|r|r|}" << endl;
1925 ost <<
"\\hline" << endl;
1926 ost <<
"i & T_1[i] & T_2[i] \\\\" << endl;
1927 ost <<
"\\hline" << endl;
1928 ost <<
"\\hline" << endl;
1930 for (i = 0; i < n; i++) {
1931 ost << i + 1 <<
" & " << Table1[i] <<
" & "
1932 << Table2[i] <<
"\\\\" << endl;
1933 if ((i + 1) % 10 == 0) {
1934 ost <<
"\\hline" << endl;
1935 ost <<
"\\end{array}" << endl;
1936 ost <<
"\\quad" << endl;
1937 ost <<
"\\begin{array}[t]{|r|r|r|}" << endl;
1938 ost <<
"\\hline" << endl;
1939 ost <<
"i & T_1[i] & T_2[i] \\\\" << endl;
1940 ost <<
"\\hline" << endl;
1941 ost <<
"\\hline" << endl;
1944 ost <<
"\\hline" << endl;
1945 ost <<
"\\end{array}" << endl;
1946 ost <<
"$$" << endl;
1951 int factorbase,
int verbose_level)
1953 int f_v = (verbose_level >= 1);
1954 int i, from, to, l, unit_size = 1000;
1957 cout <<
"number_theory_domain::sieve" << endl;
1960 for (i = 0; ; i++) {
1961 from = i * unit_size + 1;
1962 to = from + unit_size - 1;
1965 cout <<
"[" << from <<
"," << to
1966 <<
"], total number of primes = "
1968 if (l >= factorbase) {
1974 cout <<
"number_theory_domain::sieve done" << endl;
1979 int from,
int to,
int limit,
int verbose_level)
1981 int f_v = (verbose_level >= 1);
1982 int x, y, l, k, p, f_prime;
1985 cout <<
"number_theory_domain::sieve_primes" << endl;
1994 for (; x <= to; x++, x++) {
1999 for (k = 0; k < l; k++) {
2003 if ((x - y * p) == 0) {
2019 cout <<
"error: " << x <<
" is not prime!" << endl;
2023 cout << v.size() <<
" " << x << endl;
2031 cout <<
"number_theory_domain::sieve_primes done" << endl;
2054 int a,
int q,
int n,
int verbose_level)
2056 int f_v = (verbose_level >= 1);
2060 cout <<
"number_theory_domain::cyclotomic_set" << endl;
2065 cout <<
"push " << b << endl;
2075 cout <<
"push " << b << endl;
2079 cout <<
"number_theory_domain::cyclotomic_set done" << endl;
2086 int x1,
int x2,
int x3,
2087 int y1,
int y2,
int y3,
2088 int &z1,
int &z2,
int &z3,
int verbose_level)
2090 int f_v = (verbose_level >= 1);
2091 int a, two, three, top, bottom, m;
2094 cout <<
"number_theory_domain::elliptic_curve_addition" << endl;
2112 x1 = F->
mult(x1, a);
2113 x2 = F->
mult(x2, a);
2117 y1 = F->
mult(y1, a);
2118 y2 = F->
mult(y2, a);
2120 if (x1 == y1 && x2 != y2) {
2121 if (F->
negate(x2) != y2) {
2122 cout <<
"x1 == y1 && x2 != y2 && negate(x2) != y2" << endl;
2130 if (x1 == y1 && x2 == 0 && y2 == 0) {
2136 if (x1 == y1 && x2 == y2) {
2138 three = F->
add(two, 1);
2140 bottom = F->
mult(two, x2);
2142 m = F->
mult(top, a);
2148 m = F->
mult(top, a);
2155 cout <<
"number_theory_domain::elliptic_curve_addition done" << endl;
2160 int b,
int c,
int n,
2161 int x1,
int y1,
int z1,
2162 int &x3,
int &y3,
int &z3,
2165 int f_v = (verbose_level >= 1);
2171 cout <<
"number_theory_domain::elliptic_curve_point_multiple" << endl;
2187 tx, ty, tz, verbose_level - 1);
2198 tx, ty, tz, verbose_level - 1);
2210 cout <<
"number_theory_domain::elliptic_curve_point_multiple done" << endl;
2216 int b,
int c,
int n,
2217 int x1,
int y1,
int z1,
2218 int &x3,
int &y3,
int &z3,
2221 int f_v = (verbose_level >= 1);
2227 cout <<
"number_theory_domain::elliptic_curve_point_multiple_with_log" << endl;
2235 cout <<
"ECMultiple$\\Big((" << bx <<
"," << by <<
"," << bz <<
"),";
2236 cout <<
"(" << cx <<
"," << cy <<
"," << cz <<
"),"
2237 << n <<
"," << b <<
"," << c <<
"," << F->
p <<
"\\Big)$\\\\" << endl;
2247 tx, ty, tz, verbose_level - 1);
2258 tx, ty, tz, verbose_level - 1);
2264 cout <<
"=ECMultiple$\\Big((" << bx <<
"," << by <<
"," << bz <<
"),";
2265 cout <<
"(" << cx <<
"," << cy <<
"," << cz <<
"),"
2266 << n <<
"," << b <<
"," << c <<
"," << F->
p <<
"\\Big)$\\\\" << endl;
2272 cout <<
"$=(" << x3 <<
"," << y3 <<
"," << z3 <<
")$\\\\" << endl;
2274 cout <<
"number_theory_domain::elliptic_curve_point_multiple_with_log done" << endl;
2280 int x,
int b,
int c)
2285 x3 = F->
mult(x2, x);
2286 e = F->
add(x3, F->
mult(b, x));
2292 int b,
int c,
int &nb,
int *&T,
int verbose_level)
2294 int f_v = (verbose_level >= 1);
2302 cout <<
"number_theory_domain::elliptic_curve_points" << endl;
2306 for (x = 0; x < F->
p; x++) {
2310 cout << nb <<
" : (" << x <<
"," << 0 <<
",1)" << endl;
2317 cout <<
"number_theory_domain::elliptic_curve_points odd characteristic and e > 1" << endl;
2320 l =
Legendre(r, F->
p, verbose_level - 1);
2326 cout << nb <<
" : (" << x <<
"," << y <<
",1)" << endl;
2327 cout << nb + 1 <<
" : (" << x <<
"," << F->
negate(y) <<
",1)" << endl;
2335 cout << nb <<
" : (" << x <<
"," << y <<
",1)" << endl;
2342 cout << nb <<
" : (0,1,0)" << endl;
2346 cout <<
"the curve has " << nb <<
" points" << endl;
2350 for (x = 0; x < F->
p; x++) {
2362 l =
Legendre(r, F->
p, verbose_level - 1);
2372 T[n * 3 + 1] = F->
negate(y);
2396 cout <<
"number_theory_domain::elliptic_curve_points done" << endl;
2397 cout <<
"the curve has " << nb <<
" points" << endl;
2402 int b,
int c,
int &order,
2403 int x1,
int y1,
int z1,
2404 std::vector<std::vector<int> > &Pts,
2407 int f_v = (verbose_level >= 1);
2412 cout <<
"number_theory_domain::elliptic_curve_all_point_multiples" << endl;
2446 cout <<
"number_theory_domain::elliptic_curve_all_point_multiples done" << endl;
2452 int x1,
int y1,
int z1,
2453 int x3,
int y3,
int z3,
2456 int f_v = (verbose_level >= 1);
2462 cout <<
"number_theory_domain::elliptic_curve_discrete_log" << endl;
2470 if (x2 == x3 && y2 == y3 && z2 == z3) {
2487 cout <<
"number_theory_domain::elliptic_curve_discrete_log done" << endl;
2494 int f_v = (verbose_level >= 1);
2501 cout <<
"number_theory_domain::eulers_totient_function" << endl;
2503 N.
create(n, __FILE__, __LINE__);
2505 R.
create(1, __FILE__, __LINE__);
2509 A.
create(p, __FILE__, __LINE__);
2512 cout <<
"p^e=" << A << endl;
2514 B.
create(p, __FILE__, __LINE__);
2517 cout <<
"p^{e-1}=" << A << endl;
2522 cout <<
"p^e-p^{e-1}=" << C << endl;
2528 cout <<
"number_theory_domain::eulers_totient_function done" << endl;
a collection of functions related to sorted vectors
void lint_vec_heapsort(long int *v, int len)
int frobenius_power(int a, int frob_power)
basic number theoretic functions
void factor_lint(long int a, std::vector< long int > &primes, std::vector< int > &exponents)
int random_integer_in_interval(int lower_bound, int upper_bound)
int sp_ge(int n, int p_min)
int Legendre_with_key_in_latex(std::ostream &ost, long int a, long int m, int verbose_level)
void sieve(std::vector< int > &primes, int factorbase, int verbose_level)
int Jacobi(long int a, long int m, int verbose_level)
int smallest_primedivisor(int n)
long int mod(long int a, long int p)
long int mult_mod(long int a, long int b, long int p)
void print_longfactorization(int nb_primes, ring_theory::longinteger_object *primes, int *exponents)
long int ChineseRemainder2(long int a1, long int a2, long int p1, long int p2, int verbose_level)
void do_eulerfunction_interval(long int n_min, long int n_max, int verbose_level)
void elliptic_curve_addition(field_theory::finite_field *F, int b, int c, int x1, int x2, int x3, int y1, int y2, int y3, int &z1, int &z2, int &z3, int verbose_level)
void elliptic_curve_all_point_multiples(field_theory::finite_field *F, int b, int c, int &order, int x1, int y1, int z1, std::vector< std::vector< int > > &Pts, int verbose_level)
int choose_a_prime_greater_than(int lower_bound)
void sieve_primes(std::vector< int > &v, int from, int to, int limit, int verbose_level)
long int gcd_with_key_in_latex(std::ostream &ost, long int a, long int b, int f_key, int verbose_level)
long int primitive_root_randomized(long int p, int verbose_level)
int random_integer_greater_than(int n, int lower_bound)
int choose_a_prime_in_interval(int lower_bound, int upper_bound)
long int gcd_lint(long int m, long int n)
int i_power_j_safe(int i, int j)
void elliptic_curve_point_multiple_with_log(field_theory::finite_field *F, int b, int c, int n, int x1, int y1, int z1, int &x3, int &y3, int &z3, int verbose_level)
long int power_mod(long int a, long int n, long int p)
int Legendre(long int a, long int p, int verbose_level)
void print_factorization(int nb_primes, int *primes, int *exponents)
void cyclotomic_set(std::vector< int > &cyclotomic_set, int a, int q, int n, int verbose_level)
int i_power_j(int i, int j)
long int ab_over_c(long int a, long int b, long int c)
long int i_power_j_lint(long int i, long int j)
int is_prime_power(int q)
long int add_mod(long int a, long int b, long int p)
int eulers_totient_function(int n, int verbose_level)
long int int_negate(long int a, long int p)
long int moebius_function(long int n)
int nb_primes_available()
void factor_prime_power(int q, int &p, int &e)
long int i_power_j_lint_safe(int i, int j, int verbose_level)
void extended_gcd_lint(long int m, long int n, long int &g, long int &u, long int &v)
void do_babystep_giantstep(long int p, long int g, long int h, int f_latex, std::ostream &ost, int verbose_level)
int elliptic_curve_discrete_log(field_theory::finite_field *F, int b, int c, int x1, int y1, int z1, int x3, int y3, int z3, int verbose_level)
void elliptic_curve_point_multiple(field_theory::finite_field *F, int b, int c, int n, int x1, int y1, int z1, int &x3, int &y3, int &z3, int verbose_level)
int ny2(long int x, long int &x1)
int lint_logq(long int n, int q)
long int primitive_root(long int p, int verbose_level)
int ny_p(long int n, long int p)
int int_logq(int n, int q)
long int euler_function(long int n)
void int_mult_fractions(int at, int ab, int bt, int bb, int &ct, int &cb, int verbose_level)
int Jacobi_with_key_in_latex(std::ostream &ost, long int a, long int m, int verbose_level)
int is_strict_prime_power(int q)
long int order_mod_p(long int a, long int p)
void elliptic_curve_points(field_theory::finite_field *F, int b, int c, int &nb, int *&T, int verbose_level)
int lint_log10(long int n)
int elliptic_curve_evaluate_RHS(field_theory::finite_field *F, int x, int b, int c)
void int_add_fractions(int at, int ab, int bt, int bb, int &ct, int &cb, int verbose_level)
long int int_abs(long int a)
int get_prime_from_table(int idx)
void extended_gcd_int(int m, int n, int &g, int &u, int &v)
long int inverse_mod(long int a, long int p)
int factor_int(int a, int *&primes, int *&exponents)
a collection of functions related to file io
void lint_matrix_write_csv_override_headers(std::string &fname, std::string *headers, long int *M, int m, int n)
long int file_size(std::string &fname)
interface to system functions
int random_integer(int p)
void time_check_delta(std::ostream &ost, int dt)
domain to compute with objects of type longinteger
void extended_gcd(longinteger_object &a, longinteger_object &b, longinteger_object &g, longinteger_object &u, longinteger_object &v, int verbose_level)
int square_root_mod(int a, int p, int verbose_level)
void power_longint_mod(longinteger_object &a, longinteger_object &n, longinteger_object &m, int verbose_level)
void add(longinteger_object &a, longinteger_object &b, longinteger_object &c)
void mult(longinteger_object &a, longinteger_object &b, longinteger_object &c)
void integral_division_by_lint(longinteger_object &a, long int b, longinteger_object &q, long int &r)
void mult_mod(longinteger_object &a, longinteger_object &b, longinteger_object &c, longinteger_object &m, int verbose_level)
void factor(longinteger_object &a, int &nb_primes, int *&primes, int *&exponents, int verbose_level)
void power_int(longinteger_object &a, int n)
a class to represent arbitrary precision integers
void assign_to(longinteger_object &b)
void create(long int i, const char *file, int line)
#define Lint_vec_copy(A, B, C)
int NormRemainder(int a, int m)
the orbiter library for the classification of combinatorial objects