Loading [MathJax]/extensions/tex2jax.js
Parallel numerical verification of the σ_odd problem  October 6, 2018
All Classes Namespaces Files Functions Variables Typedefs Macros
helper__inline.hpp
Go to the documentation of this file.
1 /* -*- coding: latin-1 -*- */
2 /** \file common/sigmaodd/helper__inline.hpp (December 20, 2017)
3  *
4  * GPLv3 --- Copyright (C) 2017 Olivier Pirson
5  * http://www.opimedia.be/
6  */
7 
8 #ifndef PROGS_SRC_COMMON_SIGMAODD_HELPER__INLINE_HPP_
9 #define PROGS_SRC_COMMON_SIGMAODD_HELPER__INLINE_HPP_
10 
11 // \cond
12 #include <cassert>
13 #include <cmath>
14 // \endcond
15 
16 
17 namespace sigmaodd {
18 
19  /* *********************
20  * constexpr functions *
21  ***********************/
22 
23  constexpr
24  bool
26  return !is_odd(n);
27  }
28 
29 
30  constexpr
31  bool
33  return n & 1;
34  }
35 
36 
37  constexpr
38  nat_type
39  pow_nat(nat_type n, unsigned int k) {
40  nat_type product = 1;
41 
42  while (k != 0) {
43  if (is_even(k)) {
44  k >>= 1;
45  n *= n;
46  }
47  else {
48  --k;
49  product *= n;
50  }
51  }
52 
53  return product;
54  }
55 
56 
57  constexpr
58  double
59  square(double x) {
60  return x*x;
61  }
62 
63 
64  constexpr
65  nat_type
67  return n*n;
68  }
69 
70 
71  constexpr
72  nat_type
74  const nat_type k = n >> 1;
75 
76  // n(n + 2)/4 = k (k + 1) if n even
77  // (n - 1)(n + 1)/4 = k (k + 1) if n odd
78  return (k + 1)*k;
79  }
80 
81 
82  constexpr
83  nat_type
84  sum_geometric_progression(nat_type r, unsigned int k) {
85  return (r > 1
87  (r == 0
88  ? 1
89  : r*(k + 1)));
90  }
91 
92 
93  constexpr
94  nat_type
96  assert(r > 1);
97 
98  // 1 + r + r^2 + r^3 + ... + r^k = (r^(k + 1) - 1) / (r - 1)
99  // = (r^k - 1) / (r - 1) + r^k ! to avoid some possible overflows
100  const nat_type rk = pow_nat(r, k);
101 
102  return (rk - 1)/(r - 1) + rk;
103  }
104 
105 
106  constexpr
107  nat_type
109  const nat_type k = n;
110 
111  // n(n + 1)/2
112  return (n % 2 == 0
113  ? (k + 1)*(k >> 1)
114  : ((k + 1) >> 1)*k);
115  }
116 
117 
118  constexpr
119  nat_type
121  const nat_type k = (n + 1) >> 1;
122 
123  // n^2/4 = k^2 if n even
124  // (n + 1)^4 = k^2 if n odd
125  return square(k);
126  }
127 
128 
129 
130  /* ******************
131  * inline functions *
132  ********************/
133 
134  inline
135  nat_type
137  const nat_type root = floor_eighth_root(n);
138 
139  return (square(square(square(root))) == n
140  ? root
141  : root + 1);
142  }
143 
144 
145  inline
146  nat_type
148  const nat_type root = floor_fourth_root(n);
149 
150  return (square(square(root)) == n
151  ? root
152  : root + 1);
153  }
154 
155 
156  inline
157  nat_type
159  const nat_type root = floor_square_root(n);
160 
161  return (square(root) == n
162  ? root
163  : root + 1);
164  }
165 
166 } // namespace sigmaodd
167 
168 #endif // PROGS_SRC_COMMON_SIGMAODD_HELPER__INLINE_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 pow_nat(nat_type n, unsigned int k)
Return x^k, x power k.
nat_type ceil_fourth_root(nat_type n)
nat_type floor_fourth_root(nat_type n)
Return the fourth root of n rounded to below.
Definition: helper.cpp:52
constexpr double square(double x)
Return x*x.
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).