Loading [MathJax]/extensions/tex2jax.js
Parallel numerical verification of the σ_odd problem  October 6, 2018
All Classes Namespaces Files Functions Variables Typedefs Macros
divisors.hpp
Go to the documentation of this file.
1 /* -*- coding: latin-1 -*- */
2 /** \file common/sigmaodd/divisors.hpp (January 7, 2018)
3  * \brief
4  * Functions in link with divisor notion:
5  * sum of divisors, factorization, GCD, coprime, ...
6  *
7  * GPLv3 --- Copyright (C) 2017, 2018 Olivier Pirson
8  * http://www.opimedia.be/
9  */
10 
11 #ifndef PROGS_SRC_COMMON_SIGMAODD_DIVISORS_HPP_
12 #define PROGS_SRC_COMMON_SIGMAODD_DIVISORS_HPP_
13 
14 // \cond
15 #include <cstdint>
16 
17 #include <iostream>
18 #include <utility>
19 #include <vector>
20 // \endcond
21 
22 #include "helper.hpp"
23 
24 
25 namespace sigmaodd {
26 
27  /* ********
28  * Struct *
29  **********/
30  /** \brief
31  * Structure to represent a factor with its exponent.
32  */
33  struct FactorExp {
35  unsigned int exp;
36 
37  constexpr
38  bool
39  operator==(const FactorExp &other) const;
40 
41  constexpr
42  nat_type
43  value() const;
44  };
45 
46 
47 
48  /* ************
49  * Prototypes *
50  **************/
51  /** \brief
52  * Return "factor^exponent" representation of factor_exp.
53  */
54  std::ostream& operator<<(std::ostream &out, const FactorExp &factor_exp);
55 
56 
57  /** \brief
58  * Heuristic to return a list of factors with their exponents
59  * such that all factors are coprimes
60  * and the product == a*b.
61  * If failed to have a list at least two factors then return an empty list.
62  *
63  * @param a > 1
64  * @param b > 1
65  */
66  std::vector<FactorExp>
68 
69 
70  /** \brief
71  * Divide n by d until the result is not divisible by d,
72  * and return (result, number of divisions).
73  *
74  * @param n != 0
75  * @param d > 1
76  *
77  * @return (result, number of divisions)
78  */
79  std::pair<nat_type, unsigned int>
81 
82 
83  /** \brief
84  * Return n divided by 2 until the result is odd.
85  *
86  * @param n != 0
87  */
88  inline
89  nat_type
91 
92 
93  /** \brief
94  * Divide n by 2 until the result is odd,
95  * and return (result, number of divisions).
96  *
97  * @param n != 0
98  *
99  * @return (result, number of divisions)
100  */
101  inline
102  std::pair<nat_type, unsigned int>
104 
105 
106  /** \brief
107  * Return the first divisor of n > 1 (or 1 if n <= 1)
108  */
109  nat_type
111 
112 
113  /** \brief
114  * Return the Greatest Common Divisor of a and b.
115  *
116  * If a and b == 0,
117  * then return 0.
118  *
119  * Return the Greatest Common Divisor of a and b
120  */
121  constexpr
122  nat_type
123  gcd(nat_type a, nat_type b);
124 
125 
126  /** \brief
127  * Return true iff a and b are coprime (relatively prime).
128  */
129  constexpr
130  nat_type
132 
133 
134  /** \brief
135  * Return true iff d divide n,
136  * i.e. if n is divisible by d.
137  *
138  * @param d != 0
139  * @param n
140  */
141  constexpr
142  bool
144 
145 
146  /** \brief
147  * Return true iff at least one of
148  * 3, 7, 31 or 127
149  * is an unitary divisor of n.
150  *
151  * These numbers are Mersenne primes.
152  *
153  * List of prime Mersenne numbers:
154  * https://oeis.org/A000668
155  */
156  constexpr
157  bool
159 
160 
161  /** \brief
162  * Return true iff
163  * is_first_mersenne_prime_unitary_divide(n) or is_square(n).
164  */
165  constexpr
166  bool
168 
169 
170  /** \brief
171  * Return true iff at least one of
172  * 3, 7, 31, 127, 8191, 131071 or 524287
173  * is an unitary divisor of n.
174  *
175  * These numbers are Mersenne primes.
176  *
177  * List of prime Mersenne numbers:
178  * https://oeis.org/A000668
179  */
180  constexpr
181  bool
183 
184 
185  /** \brief
186  * Return true iff at least one of
187  * 3, 7, 31, 127, 8191, 131071, 524287, 2147483647 or 2305843009213693951
188  * is an unitary divisor of n.
189  *
190  * These numbers are all the Mersenne primes < 2^64.
191  *
192  * List of prime Mersenne numbers:
193  * https://oeis.org/A000668
194  */
195  constexpr
196  bool
198 
199 
200  /** \brief
201  * Return true iff n is a perfect square.
202  */
203  bool
204  is_square(nat_type n);
205 
206 
207  /** \brief
208  * Return true iff (d divide n) and (d NOT divide n/d).
209  *
210  * @param d != 0
211  * @param n
212  */
213  constexpr
214  bool
216 
217 
218  /** \brief
219  * Return pollard_rho(n, rand() % n, floor square root of n).
220  *
221  * @param n != 0
222  */
223  nat_type
225 
226 
227  /** \brief
228  * Heuristic to find a proper divisor of n with the Pollard's rho heuristic.
229  *
230  * random is the "random" number used by the heuristic
231  * and max_iteration the maximum number of iterations to find the divisor.
232  *
233  * If a divisor is find
234  * then return it,
235  * else return 0.
236  *
237  * @param n != 0
238  * @param random < n
239  * @param max_iteration
240  *
241  * @return 0 or 1 < (divisor of n) < n
242  */
243  nat_type
244  pollard_rho(nat_type n, nat_type random, nat_type max_iteration);
245 
246 
247  /** \brief
248  * Try pollard_rho(n) a maximum of nb_tries times
249  * and return 0 or the first divisor find.
250  *
251  * @param n != 0
252  * @param nb_tries
253  *
254  * @return 0 or 1 < (divisor of n) < n
255  */
256  nat_type
257  pollard_rho_repeat(nat_type n, nat_type nb_tries);
258 
259 
260  /** \brief
261  * Calculates the sum of all divisors of n by the factorization method
262  * and returns it.
263  *
264  * @param n != 0
265  *
266  * @return the sum of all divisors of n
267  */
268  nat_type
270 
271 
272  /** \brief
273  * Calculates the sum of all divisors of n by the naive method
274  * and returns it.
275  *
276  * @param n != 0
277  *
278  * @return the sum of all divisors of n
279  */
280  nat_type
282 
283 
284  /** \brief
285  * Calculates the sum of odd divisors of n by the factorization method
286  * and returns it.
287  *
288  * https://oeis.org/A000593
289  *
290  * @param n != 0
291  *
292  * @return the sum of all odd divisors of n
293  */
294  nat_type
296 
297 
298  /** \brief
299  * Calculates the sum of odd divisors of n by the naive method
300  * and returns it.
301  *
302  * @param n != 0
303  *
304  * @return the sum of all odd divisors of n
305  */
306  nat_type
308 
309 
310  /** \brief
311  * Return an upper bound of sum odd divisors.
312  *
313  * @param n != 0
314  */
315  nat_type
317 
318 
319  /** \brief
320  * Return an upper bound of sum odd divisors of n
321  * using the DeTemple inequalities.
322  *
323  * @param n != 0
324  */
325  nat_type
327 
328 
329  /** \brief
330  * Return an upper bound of sum odd divisors of n
331  * using the DeTemple inequalities. ???
332  *
333  * @param n != 0
334  */
335  nat_type
337 
338 
339  /** \brief
340  * Return an upper bound of sum odd divisors of n
341  * using the DeTemple inequalities
342  * and the knowledge that n have no divisors <= k.
343  *
344  * @param n != 0
345  * @param k odd <= sqrt(n)
346  */
347  nat_type
349 
350 } // namespace sigmaodd
351 
352 #include "divisors__inline.hpp"
353 
354 #endif // PROGS_SRC_COMMON_SIGMAODD_DIVISORS_HPP_
Define type and some generic functions.
nat_type sum_odd_divisors__naive(nat_type n)
Calculates the sum of odd divisors of n by the naive method and returns it.
Definition: divisors.cpp:507
uint64_t nat_type
Type for natural number used in all code, on 64 bits.
Definition: helper.hpp:33
Structure to represent a factor with its exponent.
Definition: divisors.hpp:33
constexpr bool is_unitary_divide(nat_type d, nat_type n)
Return true iff (d divide n) and (d NOT divide n/d).
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. ???
Definition: divisors.cpp:635
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.
Definition: divisors.cpp:341
nat_type sum_divisors__naive(nat_type n)
Calculates the sum of all divisors of n by the naive method and returns it.
Definition: divisors.cpp:422
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.
nat_type first_divisor(nat_type n)
Return the first divisor of n > 1 (or 1 if n <= 1)
Definition: divisors.cpp:223
unsigned int exp
Definition: divisors.hpp:35
bool is_square(nat_type n)
Return true iff n is a perfect square.
Definition: divisors.cpp:267
A lot of functions and stuffs to deal the sigma_odd problem and related stuffs.
Definition: divisors.cpp:22
std::ostream & operator<<(std::ostream &out, const FactorExp &factor_exp)
Return "factor^exponent" representation of factor_exp.
Definition: divisors.cpp:148
constexpr bool is_mersenne_prime_unitary_divide(nat_type n)
Return true iff at least one of 3, 7, 31, 127, 8191, 131071, 524287, 2147483647 or 230584300921369395...
constexpr bool is_divide(nat_type d, nat_type n)
Return true iff d divide n, i.e. if n is divisible by d.
constexpr bool operator==(const FactorExp &other) const
nat_type sum_odd_divisors__factorize(nat_type n)
Calculates the sum of odd divisors of n by the factorization method and returns it.
Definition: divisors.cpp:444
nat_type sum_odd_divisors_upper_bound(nat_type n)
Return an upper bound of sum odd divisors.
Definition: divisors.cpp:531
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.
Definition: divisors.cpp:357
constexpr bool is_first_mersenne_prime_unitary_divide_or_square(nat_type n)
Return true iff is_first_mersenne_prime_unitary_divide(n) or is_square(n).
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...
Definition: divisors.cpp:162
constexpr bool is_little_mersenne_prime_unitary_divide(nat_type n)
Return true iff at least one of 3, 7, 31, 127, 8191, 131071 or 524287 is an unitary divisor of n...
constexpr bool is_first_mersenne_prime_unitary_divide(nat_type n)
Return true iff at least one of 3, 7, 31 or 127 is an unitary divisor of n.
constexpr nat_type value() const
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)...
Definition: divisors.cpp:207
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.
Definition: divisors.cpp:602
nat_type pollard_rho(nat_type n)
Return pollard_rho(n, rand() % n, floor square root of n).
Definition: divisors.cpp:292