ntheory.h
Go to the documentation of this file.
1 
7 #ifndef SYMENGINE_NTHEORY_H
8 #define SYMENGINE_NTHEORY_H
9 
10 #include <symengine/integer.h>
11 
12 namespace SymEngine
13 {
14 
15 // Prime Functions
17 SYMENGINE_EXPORT int probab_prime_p(const Integer &a, unsigned reps = 25);
19 SYMENGINE_EXPORT RCP<const Integer> nextprime(const Integer &a);
20 
21 // Basic Number-theoretic functions
23 SYMENGINE_EXPORT RCP<const Integer> gcd(const Integer &a, const Integer &b);
25 SYMENGINE_EXPORT RCP<const Integer> lcm(const Integer &a, const Integer &b);
27 SYMENGINE_EXPORT
28 void gcd_ext(const Ptr<RCP<const Integer>> &g, const Ptr<RCP<const Integer>> &s,
29  const Ptr<RCP<const Integer>> &t, const Integer &a,
30  const Integer &b);
32 SYMENGINE_EXPORT RCP<const Integer> mod(const Integer &n, const Integer &d);
34 SYMENGINE_EXPORT RCP<const Integer> quotient(const Integer &n,
35  const Integer &d);
37 SYMENGINE_EXPORT
38 void quotient_mod(const Ptr<RCP<const Integer>> &q,
39  const Ptr<RCP<const Integer>> &r, const Integer &a,
40  const Integer &b);
42 SYMENGINE_EXPORT RCP<const Integer> mod_f(const Integer &n, const Integer &d);
44 SYMENGINE_EXPORT RCP<const Integer> quotient_f(const Integer &n,
45  const Integer &d);
47 SYMENGINE_EXPORT
48 void quotient_mod_f(const Ptr<RCP<const Integer>> &q,
49  const Ptr<RCP<const Integer>> &r, const Integer &a,
50  const Integer &b);
52 SYMENGINE_EXPORT
53 int mod_inverse(const Ptr<RCP<const Integer>> &b, const Integer &a,
54  const Integer &m);
55 
57 SYMENGINE_EXPORT
58 bool crt(const Ptr<RCP<const Integer>> &R,
59  const std::vector<RCP<const Integer>> &rem,
60  const std::vector<RCP<const Integer>> &mod);
61 
63 SYMENGINE_EXPORT RCP<const Integer> fibonacci(unsigned long n);
64 
66 SYMENGINE_EXPORT
67 void fibonacci2(const Ptr<RCP<const Integer>> &g,
68  const Ptr<RCP<const Integer>> &s, unsigned long n);
69 
71 SYMENGINE_EXPORT RCP<const Integer> lucas(unsigned long n);
72 
74 SYMENGINE_EXPORT
75 void lucas2(const Ptr<RCP<const Integer>> &g, const Ptr<RCP<const Integer>> &s,
76  unsigned long n);
77 
79 SYMENGINE_EXPORT RCP<const Integer> binomial(const Integer &n, unsigned long k);
80 
82 SYMENGINE_EXPORT RCP<const Integer> factorial(unsigned long n);
83 
85 SYMENGINE_EXPORT bool divides(const Integer &a, const Integer &b);
86 
89 SYMENGINE_EXPORT int factor(const Ptr<RCP<const Integer>> &f, const Integer &n,
90  double B1 = 1.0);
91 
94 SYMENGINE_EXPORT int factor_trial_division(const Ptr<RCP<const Integer>> &f,
95  const Integer &n);
96 
98 SYMENGINE_EXPORT int factor_lehman_method(const Ptr<RCP<const Integer>> &f,
99  const Integer &n);
100 
102 SYMENGINE_EXPORT int factor_pollard_pm1_method(const Ptr<RCP<const Integer>> &f,
103  const Integer &n,
104  unsigned B = 10,
105  unsigned retries = 5);
106 
108 SYMENGINE_EXPORT int factor_pollard_rho_method(const Ptr<RCP<const Integer>> &f,
109  const Integer &n,
110  unsigned retries = 5);
111 
113 SYMENGINE_EXPORT void prime_factors(std::vector<RCP<const Integer>> &primes,
114  const Integer &n);
116 SYMENGINE_EXPORT void prime_factor_multiplicities(map_integer_uint &primes,
117  const Integer &n);
118 
121 SYMENGINE_EXPORT RCP<const Number> bernoulli(unsigned long n);
123 SYMENGINE_EXPORT RCP<const Number> harmonic(unsigned long n, long m = 1);
125 // Primitive root calculated is the smallest when n is prime.
126 SYMENGINE_EXPORT bool primitive_root(const Ptr<RCP<const Integer>> &g,
127  const Integer &n);
130 SYMENGINE_EXPORT void
131 primitive_root_list(std::vector<RCP<const Integer>> &roots, const Integer &n);
133 SYMENGINE_EXPORT RCP<const Integer> totient(const RCP<const Integer> &n);
135 SYMENGINE_EXPORT
136 RCP<const Integer> carmichael(const RCP<const Integer> &n);
138 SYMENGINE_EXPORT
139 bool multiplicative_order(const Ptr<RCP<const Integer>> &o,
140  const RCP<const Integer> &a,
141  const RCP<const Integer> &n);
143 SYMENGINE_EXPORT int legendre(const Integer &a, const Integer &n);
145 SYMENGINE_EXPORT int jacobi(const Integer &a, const Integer &n);
147 SYMENGINE_EXPORT int kronecker(const Integer &a, const Integer &n);
149 SYMENGINE_EXPORT
150 void nthroot_mod_list(std::vector<RCP<const Integer>> &roots,
151  const RCP<const Integer> &a, const RCP<const Integer> &n,
152  const RCP<const Integer> &m);
154 SYMENGINE_EXPORT
155 bool nthroot_mod(const Ptr<RCP<const Integer>> &root,
156  const RCP<const Integer> &a, const RCP<const Integer> &n,
157  const RCP<const Integer> &m);
160 SYMENGINE_EXPORT
161 bool powermod(const Ptr<RCP<const Integer>> &powm, const RCP<const Integer> &a,
162  const RCP<const Number> &b, const RCP<const Integer> &m);
165 SYMENGINE_EXPORT
166 void powermod_list(std::vector<RCP<const Integer>> &pows,
167  const RCP<const Integer> &a, const RCP<const Number> &b,
168  const RCP<const Integer> &m);
169 
171 SYMENGINE_EXPORT vec_integer_class quadratic_residues(const Integer &a);
172 
174 SYMENGINE_EXPORT bool is_quad_residue(const Integer &a, const Integer &p);
176 SYMENGINE_EXPORT bool is_nth_residue(const Integer &a, const Integer &n,
177  const Integer &mod);
179 // mu(n) = 1 if n is a square-free positive integer with an even number of prime
180 // factors
181 // mu(n) = −1 if n is a square-free positive integer with an odd number of prime
182 // factors
183 // mu(n) = 0 if n has a squared prime factor
184 SYMENGINE_EXPORT int mobius(const Integer &a);
185 // Mertens Function
186 // mertens(n) -> Sum of mobius(i) for i from 1 to n
187 SYMENGINE_EXPORT long mertens(const unsigned long a);
188 
189 SYMENGINE_EXPORT
190 integer_class mp_polygonal_number(const integer_class &s,
191  const integer_class &n);
192 SYMENGINE_EXPORT
193 integer_class mp_principal_polygonal_root(const integer_class &s,
194  const integer_class &x);
195 
206 SYMENGINE_EXPORT
207 std::pair<integer_class, integer_class>
208 mp_perfect_power_decomposition(const integer_class &n,
209  bool lowest_exponent = false);
210 
211 } // namespace SymEngine
212 
213 #endif
Main namespace for SymEngine package.
Definition: add.cpp:19
RCP< const Integer > gcd(const Integer &a, const Integer &b)
Greatest Common Divisor.
Definition: ntheory.cpp:32
void fibonacci2(const Ptr< RCP< const Integer >> &g, const Ptr< RCP< const Integer >> &s, unsigned long n)
Fibonacci n and n-1.
Definition: ntheory.cpp:118
RCP< const Integer > quotient_f(const Integer &n, const Integer &d)
Definition: ntheory.cpp:94
integer_class mp_principal_polygonal_root(const integer_class &s, const integer_class &x)
Numeric calculation of the principal s-gonal root of x.
Definition: ntheory.cpp:1669
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.
Definition: ntheory.cpp:1372
RCP< const Integer > mod_f(const Integer &n, const Integer &d)
modulo round toward -inf
Definition: ntheory.cpp:87
int factor(const Ptr< RCP< const Integer >> &f, const Integer &n, double B1)
Definition: ntheory.cpp:371
RCP< const Number > bernoulli(unsigned long n)
Definition: ntheory.cpp:496
RCP< const Integer > binomial(const Integer &n, unsigned long k)
Binomial Coefficient.
Definition: ntheory.cpp:146
bool primitive_root(const Ptr< RCP< const Integer >> &g, const Integer &n)
Computes a primitive root. Returns false if no primitive root exists.
Definition: ntheory.cpp:681
RCP< const Integer > nextprime(const Integer &a)
Definition: ntheory.cpp:173
RCP< const Integer > quotient(const Integer &n, const Integer &d)
Definition: ntheory.cpp:72
void powermod_list(std::vector< RCP< const Integer >> &pows, const RCP< const Integer > &a, const RCP< const Number > &b, const RCP< const Integer > &m)
Definition: ntheory.cpp:1469
vec_integer_class quadratic_residues(const Integer &a)
Finds all Quadratic Residues of a Positive Integer.
Definition: ntheory.cpp:1504
integer_class mp_polygonal_number(const integer_class &s, const integer_class &n)
Numeric calculation of the n:th s-gonal number.
Definition: ntheory.cpp:1652
void quotient_mod(const Ptr< RCP< const Integer >> &q, const Ptr< RCP< const Integer >> &r, const Integer &n, const Integer &d)
Definition: ntheory.cpp:77
int factor_pollard_rho_method(const Ptr< RCP< const Integer >> &f, const Integer &n, unsigned retries)
Factor using Pollard's rho methods.
Definition: ntheory.cpp:349
std::pair< integer_class, integer_class > mp_perfect_power_decomposition(const integer_class &n, bool lowest_exponent)
Decompose a positive integer into perfect powers.
Definition: ntheory.cpp:1680
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.
Definition: ntheory.cpp:297
int factor_trial_division(const Ptr< RCP< const Integer >> &f, const Integer &n)
Definition: ntheory.cpp:422
RCP< const Integer > fibonacci(unsigned long n)
Fibonacci number.
Definition: ntheory.cpp:111
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.
Definition: ntheory.cpp:1402
RCP< const Integer > carmichael(const RCP< const Integer > &n)
Carmichael function.
Definition: ntheory.cpp:813
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.
Definition: ntheory.cpp:553
RCP< const Integer > lucas(unsigned long n)
Lucas number.
Definition: ntheory.cpp:128
RCP< const Number > harmonic(unsigned long n, long m)
Computes the sum of the inverses of the first perfect mth powers.
Definition: ntheory.cpp:523
void quotient_mod_f(const Ptr< RCP< const Integer >> &q, const Ptr< RCP< const Integer >> &r, const Integer &n, const Integer &d)
Definition: ntheory.cpp:101
RCP< const Integer > factorial(unsigned long n)
Factorial.
Definition: ntheory.cpp:154
int kronecker(const Integer &a, const Integer &n)
Kronecker Function.
Definition: ntheory.cpp:883
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.
Definition: ntheory.cpp:843
bool divides(const Integer &a, const Integer &b)
Definition: ntheory.cpp:162
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.
Definition: ntheory.cpp:39
void lucas2(const Ptr< RCP< const Integer >> &g, const Ptr< RCP< const Integer >> &s, unsigned long n)
Lucas number n and n-1.
Definition: ntheory.cpp:135
void prime_factor_multiplicities(map_integer_uint &primes_mul, const Integer &n)
Find multiplicities of prime factors of n
Definition: ntheory.cpp:462
void prime_factors(std::vector< RCP< const Integer >> &prime_list, const Integer &n)
Find prime factors of n
Definition: ntheory.cpp:432
RCP< const Integer > totient(const RCP< const Integer > &n)
Euler's totient function.
Definition: ntheory.cpp:793
bool is_nth_residue(const Integer &a, const Integer &n, const Integer &mod)
Returns true if 'a' is a nth power residue of 'mod'.
Definition: ntheory.cpp:1573
int factor_lehman_method(const Ptr< RCP< const Integer >> &f, const Integer &n)
Factor using lehman's methods.
Definition: ntheory.cpp:254
void primitive_root_list(std::vector< RCP< const Integer >> &roots, const Integer &n)
Definition: ntheory.cpp:765
bool is_quad_residue(const Integer &a, const Integer &p)
Returns true if 'a' is a quadratic residue of 'p'.
Definition: ntheory.cpp:1529
int mobius(const Integer &a)
Mobius Function.
Definition: ntheory.cpp:1608
bool powermod(const Ptr< RCP< const Integer >> &powm, const RCP< const Integer > &a, const RCP< const Number > &b, const RCP< const Integer > &m)
Definition: ntheory.cpp:1434
int legendre(const Integer &a, const Integer &n)
Legendre Function.
Definition: ntheory.cpp:873
RCP< const Integer > mod(const Integer &n, const Integer &d)
modulo round toward zero
Definition: ntheory.cpp:67
int probab_prime_p(const Integer &a, unsigned reps)
Probabilistic Prime.
Definition: ntheory.cpp:168
int mod_inverse(const Ptr< RCP< const Integer >> &b, const Integer &a, const Integer &m)
inverse modulo
Definition: ntheory.cpp:57
int jacobi(const Integer &a, const Integer &n)
Jacobi Function.
Definition: ntheory.cpp:878
RCP< const Integer > lcm(const Integer &a, const Integer &b)
Least Common Multiple.
Definition: ntheory.cpp:50