16 #include "../helper/helper.hpp" 52 return std::vector<nat_type>{a};
69 return std::vector<nat_type>{a};
89 return std::vector<nat_type>{a, b};
105 return std::vector<nat_type>{d, b};
117 if (is_coprime_d_a && is_coprime_d_b) {
118 return std::vector<nat_type>{d, a, b};
121 if (is_coprime_d_b) {
122 std::swap(is_coprime_d_a, is_coprime_d_b);
126 if (is_coprime_d_a) {
127 std::vector<nat_type> factors = (d == b
128 ? std::vector<nat_type>{b}
131 factors.push_back(a);
149 assert(factor_exp.
exp != 0);
152 if (factor_exp.
exp > 1) {
153 out <<
'^' << factor_exp.
exp;
161 std::vector<FactorExp>
168 std::vector<FactorExp> coprimes;
173 assert(factor < a*b);
175 unsigned int alpha = 0;
183 coprimes.push_back({factor, alpha});
187 for (
auto it = coprimes.begin(); it < coprimes.end(); ++it) {
190 return std::vector<FactorExp>();
195 coprimes.push_back({n, 1});
198 assert(!coprimes.empty());
200 return (coprimes.size() > 1
202 : std::vector<FactorExp>());
206 std::pair<nat_type, unsigned int>
211 unsigned int alpha = 0;
218 return std::make_pair(n, alpha);
239 else if (prime > sqrt_n) {
251 for (
unsigned int i = 0; i < potential_prime_offsets_table_nb(); ++i) {
252 p += potential_prime_offsets_table_by_index(i);
268 if (n >= 18446744065119617025ul) {
269 return (n == 18446744065119617025ul);
272 nat_type sqrt_n =
static_cast<nat_type>(floor(sqrt(static_cast<double>(n))));
278 }
while (
square(sqrt_n) < n);
281 while (
square(sqrt_n) > n) {
286 return (
square(sqrt_n) == n);
313 for (
nat_type i = 1; i < max_iteration; ++i) {
323 if ((d != 1) && (d != n)) {
344 while (nb_tries-- > 0) {
362 n = result_alpha.first;
371 if (prime > sqrt_n) {
375 unsigned int alpha = 0;
395 for (
unsigned int i = 0; i < potential_prime_offsets_table_nb(); ++i) {
396 p += potential_prime_offsets_table_by_index(i);
402 unsigned int alpha = 0;
429 for (
nat_type i = 1; i <= sqrt_n; ++i) {
435 if (
square(sqrt_n) == n) {
456 if (prime > sqrt_n) {
460 unsigned int alpha = 0;
480 for (
unsigned int i = 0; i < potential_prime_offsets_table_nb(); ++i) {
481 p += potential_prime_offsets_table_by_index(i);
487 unsigned int alpha = 0;
516 for (
nat_type i = 1; i <= sqrt_n; i += 2) {
522 if (
square(sqrt_n) == n) {
545 const int round = fegetround();
547 fesetround(FE_DOWNWARD);
553 for ( ; i < 10; ++i) {
556 if (prime > sqrt_n) {
567 for (
nat_type d = 3; d <= k; d += 2) {
569 diff +=
static_cast<double>(n)/static_cast<double>(d);
579 for ( ; i < 10; ++i) {
582 if (prime > sqrt_n) {
588 diff +=
static_cast<double>(n)/static_cast<double>(prime);
597 return bound -
static_cast<nat_type>(std::floor(diff));
616 const int round = fegetround();
618 fesetround(FE_UPWARD);
622 * static_cast<double>(n)));
626 return square((sqrt_n + 1)/2) + harmonic_part;
648 const int round = fegetround();
650 fesetround(FE_UPWARD);
657 return square((sqrt_n + 1)/2) + harmonic_part;
674 for (
nat_type i = 3; i <= k; i += 2) {
684 bound -=
static_cast<nat_type>((std::floor(static_cast<double>(n)*diff)));
nat_type sum_odd_divisors__naive(nat_type n)
Calculates the sum of odd divisors of n by the naive method and returns it.
constexpr nat_type sum_geometric_progression_strict(nat_type r, unsigned int k)
Return sum_geometric_progression(r, k) but only for r > 1.
nat_type floor_square_root(nat_type n)
Return the square root of n rounded to below.
uint64_t nat_type
Type for natural number used in all code, on 64 bits.
Structure to represent a factor with its exponent.
prime_type odd_primes_table_last()
Return the last odd prime number in the precalculated table.
constexpr bool is_odd(nat_type n)
Return true iff n is odd.
nat_type sum_odd_divisors_upper_bound__DeTemple_ceil(nat_type n)
Return an upper bound of sum odd divisors of n using the DeTemple inequalities. ???
nat_type pollard_rho_repeat(nat_type n, nat_type nb_tries)
Try pollard_rho(n) a maximum of nb_tries times and return 0 or the first divisor find.
nat_type sum_divisors__naive(nat_type n)
Calculates the sum of all divisors of n by the naive method and returns it.
std::pair< nat_type, unsigned int > divide_until_odd_nb(nat_type n)
Divide n by 2 until the result is odd, and return (result, number of divisions).
nat_type divide_until_odd(nat_type n)
Return n divided by 2 until the result is odd.
constexpr bool is_even(nat_type n)
Return true iff n is even.
nat_type first_divisor(nat_type n)
Return the first divisor of n > 1 (or 1 if n <= 1)
bool is_square(nat_type n)
Return true iff n is a perfect square.
double diff_half_harmonic_upper_bound(nat_type a, nat_type b)
Return an upper bound of H_a - 1/2 H_b.
std::vector< nat_type > coprime_factors_(nat_type a, nat_type b)
Return a list of factors such that all factors are coprimes and divide a*b, and these factors are (po...
A lot of functions and stuffs to deal the sigma_odd problem and related stuffs.
std::ostream & operator<<(std::ostream &out, const FactorExp &factor_exp)
Return "factor^exponent" representation of factor_exp.
constexpr bool is_divide(nat_type d, nat_type n)
Return true iff d divide n, i.e. if n is divisible by d.
Functions to access to tables of precaculated prime numbers and offsets to iterate on possible prime ...
nat_type sum_odd_divisors__factorize(nat_type n)
Calculates the sum of odd divisors of n by the factorization method and returns it.
unsigned __int128 tmp_uint128_type
Type double size for some temporary calculation.
Function to calculate harmonic number H_n = 1 + 1/2 + 1/3 + 1/4 + ... + 1/n and some variants and upp...
nat_type sum_odd_divisors_upper_bound(nat_type n)
Return an upper bound of sum odd divisors.
constexpr nat_type gcd(nat_type a, nat_type b)
Return the Greatest Common Divisor of a and b.
nat_type sum_divisors__factorize(nat_type n)
Calculates the sum of all divisors of n by the factorization method and returns it.
prime_type odd_primes_table_by_index(unsigned int i)
Return the (i + 1)th odd prime number from the precalculated table.
std::vector< FactorExp > coprime_factor_exps(nat_type a, nat_type b)
Heuristic to return a list of factors with their exponents such that all factors are coprimes and the...
constexpr unsigned int odd_primes_table_nb()
Return the number of odd prime numbers in the precalculated table.
constexpr double square(double x)
Return x*x.
Functions in link with divisor notion: sum of divisors, factorization, GCD, coprime, ...
std::pair< nat_type, unsigned int > divide_until_nb(nat_type n, nat_type d)
Divide n by d until the result is not divisible by d, and return (result, number of divisions)...
constexpr nat_type is_coprime(nat_type a, nat_type b)
Return true iff a and b are coprime (relatively prime).
nat_type sum_odd_divisors_upper_bound__DeTemple(nat_type n)
Return an upper bound of sum odd divisors of n using the DeTemple inequalities.
nat_type pollard_rho(nat_type n)
Return pollard_rho(n, rand() % n, floor square root of n).