4 #include <symengine/prime_sieve.h>
10 #ifdef HAVE_SYMENGINE_ECM
13 #ifdef HAVE_SYMENGINE_PRIMESIEVE
14 #include <primesieve.hpp>
16 #ifdef HAVE_SYMENGINE_ARB
18 #include <flint/fmpq.h>
20 #include <bernoulli.h>
22 #ifndef HAVE_SYMENGINE_GMP
23 #include <boost/random/uniform_int.hpp>
24 #include <boost/random/mersenne_twister.hpp>
25 #include <boost/random.hpp>
39 void gcd_ext(
const Ptr<RCP<const Integer>> &g,
const Ptr<RCP<const Integer>> &s,
40 const Ptr<RCP<const Integer>> &t,
const Integer &a,
43 integer_class g_, s_, t_;
78 const Ptr<RCP<const Integer>> &r,
const Integer &n,
102 const Ptr<RCP<const Integer>> &r,
const Integer &n,
105 integer_class _q, _r;
119 const Ptr<RCP<const Integer>> &s,
unsigned long n)
123 mp_fib2_ui(g_t, s_t, n);
128 RCP<const Integer>
lucas(
unsigned long n)
135 void lucas2(
const Ptr<RCP<const Integer>> &g,
const Ptr<RCP<const Integer>> &s,
140 mp_lucnum2_ui(g_t, s_t, n);
183 int _factor_trial_division_sieve(integer_class &
factor,
const integer_class &N)
185 integer_class sqrtN = mp_sqrt(N);
186 unsigned long limit = mp_get_ui(sqrtN);
188 throw SymEngineException(
"N too large to factor");
189 Sieve::iterator pi(numeric_cast<unsigned>(limit));
191 while ((p = pi.next_prime()) <= limit) {
200 int _factor_lehman_method(integer_class &rop,
const integer_class &n)
203 throw SymEngineException(
"Require n >= 21 to use lehman method");
206 integer_class u_bound;
208 mp_root(u_bound, n, 3);
209 u_bound = u_bound + 1;
211 Sieve::iterator pi(numeric_cast<unsigned>(mp_get_ui(u_bound)));
213 while ((p = pi.next_prime()) <= mp_get_ui(u_bound)) {
223 integer_class k, a, b, l;
227 while (k <= u_bound) {
228 a = mp_sqrt(4 * k * n);
235 l = a * a - 4 * k * n;
236 if (mp_perfect_square_p(l)) {
267 int _factor_pollard_pm1_method(integer_class &rop,
const integer_class &n,
268 const integer_class &c,
unsigned B)
271 throw SymEngineException(
272 "Require n > 3 and B > 2 to use Pollard's p-1 method");
277 Sieve::iterator pi(B);
279 while ((p = pi.next_prime()) <= B) {
285 mp_powm(_c, _c, m, n);
290 if (rop == 1 or rop == n)
298 const Integer &n,
unsigned B,
unsigned retries)
301 integer_class rop, nm4, c;
306 for (
unsigned i = 0; i < retries and ret_val == 0; ++i) {
307 state.urandomint(c, nm4);
320 int _factor_pollard_rho_method(integer_class &rop,
const integer_class &n,
321 const integer_class &a,
const integer_class &s,
322 unsigned steps = 10000)
325 throw SymEngineException(
"Require n > 4 to use pollard's-rho method");
327 integer_class u, v, g, m;
331 for (
unsigned i = 0; i < steps; ++i) {
350 const Integer &n,
unsigned retries)
353 integer_class rop, nm1, nm4, a, s;
358 for (
unsigned i = 0; i < retries and ret_val == 0; ++i) {
359 state.urandomint(a, nm1);
360 state.urandomint(s, nm4);
374 integer_class _n, _f;
378 #ifdef HAVE_SYMENGINE_ECM
379 if (mp_perfect_power_p(_n)) {
381 unsigned long int i = 1;
382 integer_class m, rem;
392 while (i > 1 and rem != 0) {
393 mp_rootrem(_f, rem, _n, i);
400 if (mp_probab_prime_p(_n, 25) > 0) {
405 for (
int i = 0; i < 10 and not ret_val; ++i)
406 ret_val = ecm_factor(get_mpz_t(_f), get_mpz_t(_n), B1,
nullptr);
409 throw SymEngineException(
410 "ECM failed to factor the given number");
415 ret_val = _factor_trial_division_sieve(_f, _n);
443 auto limit = mp_get_ui(sqrtN);
444 if (not mp_fits_ulong_p(sqrtN)
446 throw SymEngineException(
"N too large to factor");
450 while ((p = pi.next_prime()) <= limit) {
451 while (_n % p == 0) {
452 prime_list.push_back(
integer(p));
473 auto limit = mp_get_ui(sqrtN);
474 if (not mp_fits_ulong_p(sqrtN)
476 throw SymEngineException(
"N too large to factor");
480 while ((p = pi.next_prime()) <= limit) {
482 while (_n % p == 0) {
498 #ifdef HAVE_SYMENGINE_ARB
501 bernoulli_fmpq_ui(res, n);
504 fmpq_get_mpq(a, res);
512 for (
unsigned m = 0; m <= n; ++m) {
513 v[m] = rational_class(1u, m + 1);
515 for (
unsigned j = m; j >= 1; --j) {
516 v[j - 1] = j * (v[j - 1] - v[j]);
523 RCP<const Number>
harmonic(
unsigned long n,
long m)
525 rational_class res(0);
527 for (
unsigned i = 1; i <= n; ++i) {
528 res += rational_class(1u, i);
532 for (
unsigned i = 1; i <= n; ++i) {
534 rational_class t(1u, i);
535 #if SYMENGINE_INTEGER_CLASS != SYMENGINE_BOOSTMP
536 mp_pow_ui(get_den(t), get_den(t), m);
543 mp_pow_ui(t, t,
static_cast<unsigned long>(-m));
553 bool crt(
const Ptr<RCP<const Integer>> &R,
557 if (
mod.size() > rem.size())
558 throw SymEngineException(
"Too few remainders");
560 throw SymEngineException(
"Moduli vector cannot be empty");
562 integer_class m, r, g, s, t;
563 m =
mod[0]->as_integer_class();
564 r = rem[0]->as_integer_class();
566 for (
unsigned i = 1; i <
mod.size(); ++i) {
567 mp_gcdext(g, s, t, m,
mod[i]->as_integer_class());
569 t = rem[i]->as_integer_class() - r;
570 if (not mp_divisible_p(t, g))
572 r += m * s * (t / g);
573 m *=
mod[i]->as_integer_class() / g;
584 void _crt_cartesian(
std::vector<RCP<const Integer>> &R,
588 if (
mod.size() > rem.size())
589 throw SymEngineException(
"Too few remainders");
591 throw SymEngineException(
"Moduli vector cannot be empty");
592 integer_class m, _m, r, s, t;
593 m =
mod[0]->as_integer_class();
596 for (
unsigned i = 1; i <
mod.size(); ++i) {
598 mp_invert(s, m,
mod[i]->as_integer_class());
600 m *=
mod[i]->as_integer_class();
601 for (
auto &elem : R) {
602 for (
auto &_k : rem[i]) {
603 r = elem->as_integer_class();
604 r += _m * s * (_k->as_integer_class() - r);
615 bool _prime_power(integer_class &p, integer_class &e,
const integer_class &n)
619 integer_class _n = n, temp;
622 while (mp_perfect_power_p(_n) and _n >= 2) {
623 if (mp_root(temp, _n, i)) {
630 if (mp_probab_prime_p(_n, 25)) {
640 void _primitive_root(integer_class &g,
const integer_class &p,
641 const integer_class &e,
bool even =
false)
650 for (
const auto &it : primes) {
651 t = it->as_integer_class();
666 integer_class pm1 = p - 1;
667 mp_powm(t, g, pm1, t);
673 if (even and g % 2 == 0) {
674 mp_pow_ui(t, p, mp_get_ui(e));
701 if (not _prime_power(p, e, _n))
703 _primitive_root(_n, p, e, even);
715 void _primitive_root_list(
std::vector<RCP<const Integer>> &roots,
716 const integer_class &p,
const integer_class &e,
719 integer_class g, h, d, t, pe2, n, pm1;
720 _primitive_root(g, p, integer_class(1),
726 mp_pow_ui(n, p, mp_get_ui(e));
727 for (
unsigned long i = 1; i < p; ++i) {
730 mp_gcd(d, pm1, integer_class(i));
733 if (even and h % 2 == 0)
734 roots.push_back(
integer(h + n));
738 integer_class pp = p * p;
743 mp_powm(d, h, t, pp);
744 d = ((h - d) / p + p) % p;
747 mp_pow_ui(pe2, p, mp_get_ui(e) - 2);
748 for (
unsigned long j = 0; j < pe2; ++j) {
749 for (
unsigned long i = 0; i < p; ++i) {
751 if (even and t % 2 == 0)
752 roots.push_back(
integer(t + n));
774 roots.push_back(
integer(_n - 1));
786 if (not _prime_power(p, e, _n))
788 _primitive_root_list(roots, p, e, even);
793 RCP<const Integer>
totient(
const RCP<const Integer> &n)
798 integer_class phi = n->as_integer_class(), p;
804 for (
const auto &it : prime_mul) {
805 p = it.first->as_integer_class();
806 mp_divexact(phi, phi, p);
819 integer_class lambda, t, p;
820 unsigned multiplicity;
824 for (
const auto &it : prime_mul) {
825 p = it.first->as_integer_class();
826 multiplicity = it.second;
833 mp_lcm(lambda, lambda, t);
834 mp_pow_ui(t, p, multiplicity - 1);
844 const RCP<const Integer> &a,
845 const RCP<const Integer> &n)
847 integer_class order, p, t;
848 integer_class _a = a->as_integer_class(),
849 _n = mp_abs(n->as_integer_class());
858 order = lambda->as_integer_class();
860 for (
const auto &it : prime_mul) {
861 p = it.first->as_integer_class();
862 mp_pow_ui(t, p, it.second);
863 mp_divexact(order, order, t);
864 mp_powm(t, _a, order, _n);
866 mp_powm(t, t, p, _n);
890 bool _sqrt_mod_tonelli_shanks(integer_class &rop,
const integer_class &a,
891 const integer_class &p)
894 integer_class n, y, b, q, pm1, t(1);
897 e = numeric_cast<unsigned>(mp_scan1(pm1));
901 state.urandomint(n, p);
902 t = mp_legendre(n, p);
907 mp_powm(rop, a, t, p);
913 mp_powm(t, t, integer_class(2), p);
918 mp_pow_ui(q, integer_class(2), e - m - 1);
920 mp_powm(y, t, integer_class(2), p);
928 bool _sqrt_mod_prime(integer_class &rop,
const integer_class &a,
929 const integer_class &p)
935 int l = mp_legendre(a, p);
941 }
else if (p % 4 == 3) {
943 mp_powm(rop, a, t, p);
944 }
else if (p % 8 == 5) {
949 mp_powm(rop, a, t, p);
952 integer_class t1 = 4 * a;
953 mp_powm(t, t1, t, p);
954 rop = (2 * a * t) % p;
958 integer_class sq = integer_class(1), _a;
960 for (
unsigned i = 1; i < p; ++i) {
966 mp_fdiv_r(sq, sq, p);
970 return _sqrt_mod_tonelli_shanks(rop, a, p);
979 void _discrete_log(integer_class &
log,
const integer_class &a,
980 const integer_class &g,
const integer_class &n,
981 const integer_class &q,
const unsigned &k,
982 const integer_class &p)
985 integer_class
gamma = a, alpha, _n, t,
beta, qj(1), m, l;
987 mp_powm(alpha, g, _n, p);
993 integer_class alpha_j(1), d, s;
995 mp_powm(s, alpha, s, p);
997 for (
unsigned j = 0; j < m; ++j) {
999 alpha_j = (alpha_j * alpha) % p;
1002 for (
unsigned long j = 0; j < k; ++j) {
1007 for (
unsigned i = 0; not found && i < m; ++i) {
1008 if (table.find(
integer(d)) != table.end()) {
1009 l = i * m + table[
integer(d)];
1019 mp_powm(t, g, t, p);
1027 bool _nthroot_mod1(
std::vector<RCP<const Integer>> &roots,
1028 const integer_class &a,
const integer_class &n,
1029 const integer_class &p,
const unsigned k,
1030 bool all_roots =
false)
1032 integer_class _n, r, root, s, t, g(0), pk, m, phi;
1033 mp_pow_ui(pk, p, k);
1034 phi = pk * (p - 1) / p;
1037 mp_powm(t, a, t, pk);
1044 mp_gcdext(_n, r, s, n, t);
1046 mp_fdiv_r(r, r, t / _n);
1048 mp_powm(s, a, r, p);
1053 }
else if (_n == 2) {
1054 _sqrt_mod_prime(root, s, p);
1056 map_integer_uint prime_mul;
1058 integer_class h, q, qt, z, v, x, s1 = s;
1059 _primitive_root(g, p, integer_class(2));
1061 for (
const auto &it : prime_mul) {
1062 q = it.first->as_integer_class();
1063 mp_pow_ui(qt, q, it.second);
1066 while (h % q == 0) {
1070 mp_invert(t, h, qt);
1073 mp_powm(v, s1, x, p);
1075 if (c == it.second) {
1078 mp_powm(x, s1, h, p);
1080 mp_powm(r, g, t, p);
1081 mp_pow_ui(qt, q, c - it.second);
1082 _discrete_log(t, x, r, qt, q, c - it.second, p);
1084 mp_powm(r, g, t, p);
1094 while (r % p == 0) {
1095 mp_divexact(r, r, p);
1100 integer_class pc = n / r, pd = pc * p;
1102 mp_powm(s, root, r, p);
1105 for (
unsigned d = c + 2; d <= k; ++d) {
1108 mp_powm(t, s, t, pd);
1109 t = (a * t - s) / pc;
1119 for (
unsigned d = 2; d < 2 * k; d *= 2) {
1124 mp_powm(u, root, t, pd);
1126 mp_invert(t, t, pd);
1127 root += (s - u * root) * t;
1128 mp_fdiv_r(root, root, pd);
1130 if (m != 1 and all_roots) {
1136 _primitive_root(g, p, integer_class(2));
1138 mp_powm(t, g, t, pk);
1140 for (
unsigned j = 0; j < m; ++j) {
1141 roots.push_back(
integer(root));
1143 mp_fdiv_r(root, root, pk);
1146 roots.push_back(
integer(root));
1153 bool _is_nthroot_mod1(
const integer_class &a,
const integer_class &n,
1154 const integer_class &p,
const unsigned k)
1156 integer_class t, pk, m, phi;
1157 mp_pow_ui(pk, p, k);
1158 phi = pk * (p - 1) / p;
1161 mp_powm(t, a, t, pk);
1170 bool _nthroot_mod_prime_power(
std::vector<RCP<const Integer>> &roots,
1171 const integer_class &a,
const integer_class &n,
1172 const integer_class &p,
const unsigned k,
1173 bool all_roots =
false)
1175 integer_class pk, root;
1179 integer_class r = n, t, s, pc, pj;
1180 pk = integer_class(1) << k;
1181 unsigned c = numeric_cast<unsigned>(mp_scan1(n));
1190 if (c > 0 and a % 4 == 3) {
1193 roots.push_back(
integer(a % 4));
1194 if (all_roots and c > 0)
1202 t = integer_class(1) << (k - 2);
1203 pc = integer_class(1) << c;
1209 mp_powm(root, a, s, pk);
1210 roots.push_back(
integer(root));
1215 t = integer_class(1) << (c + 2);
1223 for (
unsigned j = c + 2; j < k; ++j) {
1225 mp_powm(t, root, pc, pj);
1229 root += integer_class(1) << (j - c);
1232 mp_powm(root, root, s, pk);
1237 for (
unsigned i = 0; i < 2; ++i) {
1238 for (
unsigned long j = 0; j < pc; ++j) {
1239 roots.push_back(
integer(root));
1245 roots.push_back(
integer(root));
1249 return _nthroot_mod1(roots, a, n, p, k, all_roots);
1253 mp_pow_ui(pk, p, k);
1258 if (not all_roots) {
1266 m = numeric_cast<unsigned>(k - 1 - (k - 1) / mp_get_ui(n));
1267 mp_pow_ui(pm, p, m);
1270 mp_divexact(_a, _a, p);
1271 while (_a % p == 0) {
1272 mp_divexact(_a, _a, p);
1275 if (r < n or r % n != 0
1276 or not _nthroot_mod_prime_power(_roots, _a, n, p, k - r,
1280 m = numeric_cast<unsigned>(r / mp_get_ui(n));
1281 mp_pow_ui(pm, p, m);
1282 if (not all_roots) {
1287 for (
auto &it : _roots) {
1288 it =
integer(it->as_integer_class() * pm);
1290 m = numeric_cast<unsigned>(r - r / mp_get_ui(n));
1291 mp_pow_ui(pm, p, m);
1294 mp_pow_ui(pkm, p, k - m);
1296 for (
const auto &it : _roots) {
1297 root = it->as_integer_class();
1298 for (
unsigned long i = 0; i < pm; ++i) {
1299 roots.push_back(
integer(root));
1309 bool _is_nthroot_mod_prime_power(
const integer_class &a,
const integer_class &n,
1310 const integer_class &p,
const unsigned k)
1316 unsigned c = numeric_cast<unsigned>(mp_scan1(n));
1323 if (c > 0 and a % 4 == 3) {
1339 t = integer_class(1) << (c + 2);
1346 return _is_nthroot_mod1(a, n, p, k);
1350 mp_pow_ui(pk, p, k);
1357 mp_divexact(_a, _a, p);
1358 while (_a % p == 0) {
1359 mp_divexact(_a, _a, p);
1362 if (r < n or r % n != 0
1363 or not _is_nthroot_mod_prime_power(_a, n, p, k - r)) {
1373 const RCP<const Integer> &a,
const RCP<const Integer> &n,
1374 const RCP<const Integer> &
mod)
1376 if (
mod->as_integer_class() <= 0) {
1378 }
else if (
mod->as_integer_class() == 1) {
1388 for (
const auto &it : prime_mul) {
1390 mp_pow_ui(_mod, it.first->as_integer_class(), it.second);
1392 ret_val = _nthroot_mod_prime_power(
1393 rem, a->as_integer_class(), n->as_integer_class(),
1394 it.first->as_integer_class(), it.second,
false);
1398 crt(root, rem, moduli);
1403 const RCP<const Integer> &a,
const RCP<const Integer> &n,
1404 const RCP<const Integer> &m)
1406 if (m->as_integer_class() <= 0) {
1408 }
else if (m->as_integer_class() == 1) {
1418 for (
const auto &it : prime_mul) {
1420 mp_pow_ui(_mod, it.first->as_integer_class(), it.second);
1423 ret_val = _nthroot_mod_prime_power(
1424 rem1, a->as_integer_class(), n->as_integer_class(),
1425 it.first->as_integer_class(), it.second,
true);
1430 _crt_cartesian(roots, rem, moduli);
1434 bool powermod(
const Ptr<RCP<const Integer>> &powm,
const RCP<const Integer> &a,
1435 const RCP<const Number> &b,
const RCP<const Integer> &m)
1437 if (is_a<Integer>(*b)) {
1438 integer_class t = down_cast<const Integer &>(*b).as_integer_class();
1439 if (b->is_negative())
1441 mp_powm(t, a->as_integer_class(), t, m->as_integer_class());
1442 if (b->is_negative()) {
1443 bool ret_val = mp_invert(t, t, m->as_integer_class());
1449 }
else if (is_a<Rational>(*b)) {
1450 RCP<const Integer> num, den, r;
1451 get_num_den(down_cast<const Rational &>(*b), outArg(num), outArg(den));
1452 if (den->is_negative()) {
1453 den = den->mulint(*minus_one);
1454 num = num->mulint(*minus_one);
1456 integer_class t = mp_abs(num->as_integer_class());
1457 mp_powm(t, a->as_integer_class(), t, m->as_integer_class());
1458 if (num->is_negative()) {
1459 bool ret_val = mp_invert(t, t, m->as_integer_class());
1470 const RCP<const Integer> &a,
const RCP<const Number> &b,
1471 const RCP<const Integer> &m)
1473 if (is_a<Integer>(*b)) {
1475 = mp_abs(down_cast<const Integer &>(*b).as_integer_class());
1476 mp_powm(t, a->as_integer_class(), t, m->as_integer_class());
1477 if (b->is_negative()) {
1478 bool ret_val = mp_invert(t, t, m->as_integer_class());
1483 }
else if (is_a<Rational>(*b)) {
1484 RCP<const Integer> num, den, r;
1485 get_num_den(down_cast<const Rational &>(*b), outArg(num), outArg(den));
1486 if (den->is_negative()) {
1487 den = den->mulint(*
integer(-1));
1488 num = num->mulint(*
integer(-1));
1490 integer_class t = num->as_integer_class();
1491 if (num->is_negative())
1493 mp_powm(t, a->as_integer_class(), t, m->as_integer_class());
1494 if (num->is_negative()) {
1495 bool ret_val = mp_invert(t, t, m->as_integer_class());
1515 throw SymEngineException(
"quadratic_residues: Input must be > 0");
1519 for (integer_class i = integer_class(0); i <= a.
as_int() / 2; i++) {
1523 sort(residue.
begin(), residue.
end());
1539 throw SymEngineException(
1540 "is_quad_residue: Second parameter must be non-zero");
1553 const RCP<const Integer> a1 =
integer(a_final);
1554 const RCP<const Integer> p1 =
integer(p2);
1560 for (
const auto &it : prime_mul) {
1561 ret_val = _is_nthroot_mod_prime_power(
1562 a1->as_integer_class(),
integer(2)->as_integer_class(),
1563 it.first->as_integer_class(), it.second);
1570 return mp_legendre(a_final, p2) == 1;
1579 integer_class _mod =
mod.as_integer_class();
1583 }
else if (_mod == 1) {
1590 RCP<const Integer> mod2 =
integer(_mod);
1595 for (
const auto &it : prime_mul) {
1596 ret_val = _is_nthroot_mod_prime_power(
1598 it.first->as_integer_class(), it.second);
1608 throw SymEngineException(
"mobius: Integer <= 0");
1611 bool is_square_free =
true;
1613 auto num_prime_factors = prime_mul.
size();
1614 for (
const auto &it : prime_mul) {
1615 int p_freq = it.second;
1617 is_square_free =
false;
1621 if (!is_square_free) {
1623 }
else if (num_prime_factors % 2 == 0) {
1630 long mertens(
const unsigned long a)
1633 for (
unsigned long i = 1; i <= a; ++i) {
1650 const integer_class &n)
1652 auto res = ((s - 2) * n * n - (s - 4) * n) / 2;
1667 const integer_class &x)
1670 mp_pow_ui(tmp, s - 4, 2);
1671 integer_class root = mp_sqrt(8 * x * (s - 2) + tmp);
1672 integer_class n = (root + s - 4) / (2 * (s - 2));
1681 unsigned long p = 2;
1682 integer_class intone, i, j, m, res;
1687 while ((intone << p) <= n) {
1692 mp_pow_ui(res, m, p);
1698 mp_pow_ui(res, i, p);
1701 if (lowest_exponent) {
Classes and functions relating to the binary operation of addition.
const integer_class & as_integer_class() const
Convert to integer_class.
signed long int as_int() const
Convert to int, raise an exception if it does not fit.
static RCP< const Number > from_mpq(const rational_class &i)
Main namespace for SymEngine package.
integer_class mp_polygonal_number(const integer_class &s, const integer_class &n)
Numeric calculation of the n:th s-gonal number.
void get_num_den(const Rational &rat, const Ptr< RCP< const Integer >> &num, const Ptr< RCP< const Integer >> &den)
returns the num and den of rational rat as RCP<const Integer>
bool divides(const Integer &a, const Integer &b)
bool powermod(const Ptr< RCP< const Integer >> &powm, const RCP< const Integer > &a, const RCP< const Number > &b, const RCP< const Integer > &m)
RCP< const Integer > nextprime(const Integer &a)
int kronecker(const Integer &a, const Integer &n)
Kronecker Function.
void fibonacci2(const Ptr< RCP< const Integer >> &g, const Ptr< RCP< const Integer >> &s, unsigned long n)
Fibonacci n and n-1.
std::enable_if< std::is_integral< T >::value, RCP< const Integer > >::type integer(T i)
void prime_factor_multiplicities(map_integer_uint &primes_mul, const Integer &n)
Find multiplicities of prime factors of n
RCP< const Basic > beta(const RCP< const Basic > &x, const RCP< const Basic > &y)
Canonicalize Beta:
RCP< const Integer > gcd(const Integer &a, const Integer &b)
Greatest Common Divisor.
bool nthroot_mod(const Ptr< RCP< const Integer >> &root, const RCP< const Integer > &a, const RCP< const Integer > &n, const RCP< const Integer > &mod)
A solution to x**n == a mod m. Return false if none exists.
bool primitive_root(const Ptr< RCP< const Integer >> &g, const Integer &n)
Computes a primitive root. Returns false if no primitive root exists.
RCP< const Integer > mod(const Integer &n, const Integer &d)
modulo round toward zero
int mobius(const Integer &a)
Mobius Function.
RCP< const Integer > lucas(unsigned long n)
Lucas number.
RCP< const Integer > quotient_f(const Integer &n, const Integer &d)
void nthroot_mod_list(std::vector< RCP< const Integer >> &roots, const RCP< const Integer > &a, const RCP< const Integer > &n, const RCP< const Integer > &m)
All Solutions to x**n == a mod m. Return false if none exists.
RCP< const Integer > fibonacci(unsigned long n)
Fibonacci number.
RCP< const Basic > gamma(const RCP< const Basic > &arg)
Canonicalize Gamma:
bool is_quad_residue(const Integer &a, const Integer &p)
Returns true if 'a' is a quadratic residue of 'p'.
RCP< const Integer > mod_f(const Integer &n, const Integer &d)
modulo round toward -inf
RCP< const Integer > quotient(const Integer &n, const Integer &d)
void prime_factors(std::vector< RCP< const Integer >> &prime_list, const Integer &n)
Find prime factors of n
int mod_inverse(const Ptr< RCP< const Integer >> &b, const Integer &a, const Integer &m)
inverse modulo
vec_integer_class quadratic_residues(const Integer &a)
Finds all Quadratic Residues of a Positive Integer.
int jacobi(const Integer &a, const Integer &n)
Jacobi Function.
RCP< const Integer > lcm(const Integer &a, const Integer &b)
Least Common Multiple.
RCP< const Integer > binomial(const Integer &n, unsigned long k)
Binomial Coefficient.
bool multiplicative_order(const Ptr< RCP< const Integer >> &o, const RCP< const Integer > &a, const RCP< const Integer > &n)
Multiplicative order. Return false if order does not exist.
void quotient_mod_f(const Ptr< RCP< const Integer >> &q, const Ptr< RCP< const Integer >> &r, const Integer &n, const Integer &d)
void insert(T1 &m, const T2 &first, const T3 &second)
std::pair< integer_class, integer_class > mp_perfect_power_decomposition(const integer_class &n, bool lowest_exponent)
Decompose a positive integer into perfect powers.
void primitive_root_list(std::vector< RCP< const Integer >> &roots, const Integer &n)
int factor_trial_division(const Ptr< RCP< const Integer >> &f, const Integer &n)
int factor_pollard_pm1_method(const Ptr< RCP< const Integer >> &f, const Integer &n, unsigned B, unsigned retries)
Factor using Pollard's p-1 method.
int probab_prime_p(const Integer &a, unsigned reps)
Probabilistic Prime.
RCP< const Basic > log(const RCP< const Basic > &arg)
Returns the Natural Logarithm from argument arg
RCP< const Integer > totient(const RCP< const Integer > &n)
Euler's totient function.
bool is_nth_residue(const Integer &a, const Integer &n, const Integer &mod)
Returns true if 'a' is a nth power residue of 'mod'.
int factor_pollard_rho_method(const Ptr< RCP< const Integer >> &f, const Integer &n, unsigned retries)
Factor using Pollard's rho methods.
bool crt(const Ptr< RCP< const Integer >> &R, const std::vector< RCP< const Integer >> &rem, const std::vector< RCP< const Integer >> &mod)
Chinese remainder function. Return true when a solution exists.
int factor(const Ptr< RCP< const Integer >> &f, const Integer &n, double B1)
RCP< const Integer > factorial(unsigned long n)
Factorial.
RCP< const Integer > carmichael(const RCP< const Integer > &n)
Carmichael function.
void gcd_ext(const Ptr< RCP< const Integer >> &g, const Ptr< RCP< const Integer >> &s, const Ptr< RCP< const Integer >> &t, const Integer &a, const Integer &b)
Extended GCD.
integer_class mp_principal_polygonal_root(const integer_class &s, const integer_class &x)
Numeric calculation of the principal s-gonal root of x.
void powermod_list(std::vector< RCP< const Integer >> &pows, const RCP< const Integer > &a, const RCP< const Number > &b, const RCP< const Integer > &m)
RCP< const Number > bernoulli(unsigned long n)
void quotient_mod(const Ptr< RCP< const Integer >> &q, const Ptr< RCP< const Integer >> &r, const Integer &n, const Integer &d)
int legendre(const Integer &a, const Integer &n)
Legendre Function.
RCP< const Number > harmonic(unsigned long n, long m)
Computes the sum of the inverses of the first perfect mth powers.
int factor_lehman_method(const Ptr< RCP< const Integer >> &f, const Integer &n)
Factor using lehman's methods.
void lucas2(const Ptr< RCP< const Integer >> &g, const Ptr< RCP< const Integer >> &s, unsigned long n)
Lucas number n and n-1.
less operator (<) for Integers