Loading [MathJax]/extensions/tex2jax.js
Parallel numerical verification of the σ_odd problem  October 6, 2018
All Classes Namespaces Files Functions Variables Typedefs Macros
helper.hpp
Go to the documentation of this file.
1 /* -*- coding: latin-1 -*- */
2 /** \file common/sigmaodd/helper.hpp (January 6, 2018)
3  * \brief
4  * Define type and some generic functions.
5  *
6  * GPLv3 --- Copyright (C) 2017, 2018 Olivier Pirson
7  * http://www.opimedia.be/
8  */
9 
10 #ifndef PROGS_SRC_COMMON_SIGMAODD_HELPER_HPP_
11 #define PROGS_SRC_COMMON_SIGMAODD_HELPER_HPP_
12 
13 // \cond
14 #include <cstdint>
15 
16 #include <string>
17 #include <vector>
18 // \endcond
19 
20 
21 /** \brief
22  * A lot of functions and stuffs to deal the sigma_odd problem and related stuffs.
23  */
24 namespace sigmaodd {
25 
26  /* *******
27  * Types *
28  *********/
29 
30  /** \brief
31  * Type for natural number used in all code, on 64 bits.
32  */
33  typedef uint64_t nat_type;
34 
35 
36  /** \brief
37  * Type double size for some temporary calculation.
38  */
39 
40 #pragma GCC diagnostic push
41 #pragma GCC diagnostic ignored "-Wpedantic"
42  typedef unsigned __int128 tmp_uint128_type;
43 #pragma GCC diagnostic pop
44 
45 
46 
47  /* **********
48  * Constant *
49  ************/
50  /** \brief
51  * Lower bound of the bigger number
52  * such that it is possible to compute the result of the sigma function.
53  */
54  constexpr nat_type MAX_POSSIBLE_N = (static_cast<nat_type>(1) << 56) - 1;
55 
56 
57 
58  /* ************
59  * Prototypes *
60  **************/
61  /** \brief
62  * Return the eighth root of n rounded to above.
63  */
64  inline
65  nat_type
66  ceil_eighth_root(nat_type n);
67 
68 
69  /** \brief
70  * Return the fourth root of n rounded to above.
71  */
72  nat_type
73  ceilx_fourth_root(nat_type n);
74 
75 
76  /** \brief
77  * Return the square root of n rounded to above.
78  */
79  inline
80  nat_type
81  ceil_square_root(nat_type n);
82 
83 
84  /** \brief
85  * Return the eighth root of n rounded to below.
86  */
87  nat_type
88  floor_eighth_root(nat_type n);
89 
90 
91  /** \brief
92  * Return the fourth root of n rounded to below.
93  */
94  nat_type
95  floor_fourth_root(nat_type n);
96 
97 
98  /** \brief
99  * Return the square root of n rounded to below.
100  */
101  nat_type
102  floor_square_root(nat_type n);
103 
104 
105  /** \brief
106  * Return true iff n is even.
107  */
108  constexpr
109  bool
110  is_even(nat_type n);
111 
112 
113  /** \brief
114  * Return true iff n is odd.
115  */
116  constexpr
117  bool
118  is_odd(nat_type n);
119 
120 
121  /** \brief
122  * Read a file that contains list of bad numbers,
123  * add after bad_table,
124  * and return the result.
125  */
126  std::vector<nat_type>
127  load_bad_table(const std::string &filename,
128  const std::vector<nat_type> &bad_table = std::vector<nat_type>());
129 
130 
131  /** \brief
132  * Read the file
133  * and extract each natural number begining a line.
134  * Return the list of these numbers.
135  */
136  std::vector<nat_type>
137  load_numbers(const std::string &filename);
138 
139 
140  /** \brief
141  * Return x^k, x power k.
142  */
143  constexpr
144  nat_type
145  pow_nat(nat_type n, unsigned int k);
146 
147 
148  /** \brief
149  * Return x*x.
150  */
151  constexpr
152  double
153  square(double x);
154 
155 
156  /** \brief
157  * Return n*n.
158  */
159  constexpr
160  nat_type
161  square(nat_type n);
162 
163 
164  /** \brief
165  * Return 2 + 4 + 6 + 8 + ... + (n or n-1) = k(k + 1) with k = floor(n/2).
166  *
167  * https://oeis.org/A110660
168  *
169  * @return 2 + 4 + 6 + 8 + ... + (n or n-1)
170  */
171  constexpr
172  nat_type
173  sum_even(nat_type n);
174 
175 
176  /** \brief
177  * Return the sum of the (k + 1) terms
178  * of the geometric progression of the common ratio r.
179  *
180  * If r is prime
181  * then the result is equal to the sum of the divisors of r.
182  *
183  * In fact calculates (r^k - 1) / (r - 1) + r^k to avoid some overflow.
184  *
185  * @return 1 + r + r^2 + r^3 + ... + r^k = (r^(k + 1) - 1) / (r - 1)
186  */
187  constexpr
188  nat_type
189  sum_geometric_progression(nat_type r, unsigned int k);
190 
191 
192  /** \brief
193  * Return sum_geometric_progression(r, k)
194  * but only for r > 1.
195  *
196  * In fact calculates (r^k - 1) / (r - 1) + r^k to avoid some overflow.
197  *
198  * @param r > 1
199  * @param k
200  *
201  * @return 1 + r + r^2 + r^3 + ... + r^k = (r^(k + 1) - 1) / (r - 1)
202  */
203  constexpr
204  nat_type
205  sum_geometric_progression_strict(nat_type r, unsigned int k);
206 
207 
208  /** \brief
209  * Return 1 + 2 + 3 + 4 + ... + n = n(n + 1)/2.
210  *
211  * https://oeis.org/A000217
212  *
213  * @return 1 + 2 + 3 + 4 + ... + n
214  */
215  constexpr
216  nat_type
217  sum_natural(nat_type n);
218 
219 
220  /** \brief
221  * Return 1 + 3 + 5 + 7 + ... + (n or n-1) = k^2 with k floor((n+1)/2).
222  *
223  * sum_odd(n - 1) = https://oeis.org/A008794
224  *
225  * @return 1 + 3 + 5 + 7 + ... + (n or n-1)
226  */
227  constexpr
228  nat_type
229  sum_odd(nat_type n);
230 
231 } // namespace sigmaodd
232 
233 #include "helper__inline.hpp"
234 
235 #endif // PROGS_SRC_COMMON_SIGMAODD_HELPER_HPP_
constexpr nat_type sum_geometric_progression(nat_type r, unsigned int k)
Return the sum of the (k + 1) terms of the geometric progression of the common ratio r...
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.
Definition: helper.cpp:76
uint64_t nat_type
Type for natural number used in all code, on 64 bits.
Definition: helper.hpp:33
nat_type ceil_square_root(nat_type n)
Return the square root of n rounded to above.
constexpr bool is_odd(nat_type n)
Return true iff n is odd.
nat_type ceil_eighth_root(nat_type n)
Return the eighth root of n rounded to above.
constexpr bool is_even(nat_type n)
Return true iff n is even.
nat_type floor_eighth_root(nat_type n)
Return the eighth root of n rounded to below.
Definition: helper.cpp:27
A lot of functions and stuffs to deal the sigma_odd problem and related stuffs.
Definition: divisors.cpp:22
constexpr nat_type MAX_POSSIBLE_N
Lower bound of the bigger number such that it is possible to compute the result of the sigma function...
Definition: helper.hpp:54
constexpr nat_type pow_nat(nat_type n, unsigned int k)
Return x^k, x power k.
nat_type ceilx_fourth_root(nat_type n)
Return the fourth root of n rounded to above.
unsigned __int128 tmp_uint128_type
Type double size for some temporary calculation.
Definition: helper.hpp:42
nat_type floor_fourth_root(nat_type n)
Return the fourth root of n rounded to below.
Definition: helper.cpp:52
std::vector< nat_type > load_numbers(const std::string &filename)
Read the file and extract each natural number begining a line. Return the list of these numbers...
Definition: helper.cpp:115
constexpr double square(double x)
Return x*x.
std::vector< nat_type > load_bad_table(const std::string &filename, const std::vector< nat_type > &bad_table)
Read a file that contains list of bad numbers, add after bad_table, and return the result...
Definition: helper.cpp:100
constexpr nat_type sum_natural(nat_type n)
Return 1 + 2 + 3 + 4 + ... + n = n(n + 1)/2.
constexpr nat_type sum_even(nat_type n)
Return 2 + 4 + 6 + 8 + ... + (n or n-1) = k(k + 1) with k = floor(n/2).
constexpr nat_type sum_odd(nat_type n)
Return 1 + 3 + 5 + 7 + ... + (n or n-1) = k^2 with k floor((n+1)/2).