Loading [MathJax]/extensions/tex2jax.js
Parallel numerical verification of the σ_odd problem  October 6, 2018
All Classes Namespaces Files Functions Variables Typedefs Macros
pentagonal.cpp
Go to the documentation of this file.
1 /* -*- coding: latin-1 -*- */
2 /** \file common/sigmaodd/pentagonal.cpp (December 20, 2017)
3  *
4  * GPLv3 --- Copyright (C) 2017 Olivier Pirson
5  * http://www.opimedia.be/
6  */
7 
8 // \cond
9 #include <cassert>
10 
11 #include <map>
12 #include <vector>
13 // \endcond
14 
15 #include "pentagonal.hpp"
16 
17 #include "../helper/helper.hpp"
18 #include "divisors.hpp"
19 #include "primes.hpp"
20 
21 
22 namespace sigmaodd {
23 
24  /* *******************
25  * Private constants *
26  *********************/
28 
30 
31 
32 
33  /* *******************
34  * Private variables *
35  *********************/
36  std::map<nat_type, nat_type> sum_divisors__euler__cache_;
37  std::vector<nat_type> sum_divisors__euler__table_(sum_divisors__euler__table_nb_);
38 
39 
40 
41  /* *******************
42  * Private prototype *
43  *********************/
44 
45  nat_type
47 
48 
49 
50  /* ******************
51  * Private function *
52  ********************/
53 
54  nat_type
56  return (n > pi
57  ? sum_divisors__euler(n - pi)
58  : (n == pi
59  ? n
60  : 0));
61  }
62 
63 
64 
65  /* ***********
66  * Functions *
67  *************/
68 
69  nat_type
71  assert(n != 0);
72 
73  if (n < sum_divisors__euler__table_.size()) {
74  if (sum_divisors__euler__table_[n] != 0) { // in the table
76  }
77  }
78  else {
79  const auto it = sum_divisors__euler__cache_.find(n);
80 
81  if (it != sum_divisors__euler__cache_.cend()) { // in the cache
82  return it->second;
83  }
84  }
85 
86  if (is_even(n)) {
87  unsigned int alpha = 0;
88 
89  do {
90  ++alpha;
91  n >>= 1;
92  } while (is_divide(2, n));
93 
95  }
96 
97  for (unsigned int i = 0; i < 10; ++i) {
98  const nat_type prime = odd_primes_table_by_index(i);
99 
100  if (is_divide(prime, n)) {
101  unsigned int alpha = 0;
102 
103  do {
104  ++alpha;
105  n /= prime;
106  } while (is_divide(prime, n));
107 
109  }
110  }
111 
112  nat_type sum = 0;
113 
114  for (nat_type i = 1; ; ++i) {
115  nat_type pi = pentagonal(i);
116  nat_type pi_neg = pentagonal_neg(i);
117 
118  if ((n < pi) && (n < pi_neg)) {
119  break;
120  }
121 
122  if (is_odd(i)) {
123  sum += sum_divisors_diff_(n, pi);
124  sum += sum_divisors_diff_(n, pi_neg);
125  }
126  else {
127  sum -= sum_divisors_diff_(n, pi);
128  sum -= sum_divisors_diff_(n, pi_neg);
129  }
130  }
131 
132  if (n < sum_divisors__euler__table_.size()) { // put in the table
134  }
135  else { // put in the cache
136  if (sum_divisors__euler_cache_is_full()) { // kill one item (at random)
137  auto it = sum_divisors__euler__cache_.begin();
138 
139  std::advance(it,
140  static_cast<unsigned int>(std::rand())
141  % (sum_divisors__euler__cache_max_nb_ - 10));
142  sum_divisors__euler__cache_.erase(it);
143  }
144  sum_divisors__euler__cache_[n] = sum;
145  }
146 
147  return sum;
148  }
149 
150 
151  bool
153  return (sum_divisors__euler__cache_.size() >= sum_divisors__euler__cache_max_nb_);
154  }
155 
156 
157  std::size_t
159  return sum_divisors__euler__cache_.size();
160  }
161 
162 
163  std::size_t
165  return sum_divisors__euler__table_.size();
166  }
167 
168 } // namespace sigmaodd
constexpr nat_type sum_geometric_progression_strict(nat_type r, unsigned int k)
Return sum_geometric_progression(r, k) but only for r > 1.
uint64_t nat_type
Type for natural number used in all code, on 64 bits.
Definition: helper.hpp:33
constexpr bool is_odd(nat_type n)
Return true iff n is odd.
Functions to calculate pentagonal numbers and one useless algorithm that used the Euler formula to ca...
constexpr bool is_even(nat_type n)
Return true iff n is even.
size_t sum_divisors__euler__table_nb_
Definition: pentagonal.cpp:29
std::size_t sum_divisors__euler_cache_nb()
Return the current number of items in the cache used by sum_divisors__euler()
Definition: pentagonal.cpp:158
constexpr nat_type pentagonal_neg(nat_type n)
Return the generalized pentagonal number of -n.
A lot of functions and stuffs to deal the sigma_odd problem and related stuffs.
Definition: divisors.cpp:22
bool sum_divisors__euler_cache_is_full()
Return true iff the cache used by sum_divisors__euler() is full. The function can clean space...
Definition: pentagonal.cpp:152
constexpr nat_type pentagonal(nat_type n)
Return the pentagonal number of n.
std::vector< nat_type > sum_divisors__euler__table_(sum_divisors__euler__table_nb_)
constexpr bool is_divide(nat_type d, nat_type n)
Return true iff d divide n, i.e. if n is divisible by d.
std::map< nat_type, nat_type > sum_divisors__euler__cache_
Definition: pentagonal.cpp:36
Functions to access to tables of precaculated prime numbers and offsets to iterate on possible prime ...
std::size_t sum_divisors__euler_table_nb()
Return the (constant) number of items in the table used by sum_divisors__euler()
Definition: pentagonal.cpp:164
prime_type odd_primes_table_by_index(unsigned int i)
Return the (i + 1)th odd prime number from the precalculated table.
size_t sum_divisors__euler__cache_max_nb_
Definition: pentagonal.cpp:27
nat_type sum_divisors__euler(nat_type n)
Calculates the sum of all divisors of n with the Euler formula about pentagonal numbers and returns i...
Definition: pentagonal.cpp:70
const double alpha
Definition: harmonic.cpp:24
Functions in link with divisor notion: sum of divisors, factorization, GCD, coprime, ...
nat_type sum_divisors_diff_(nat_type n, nat_type pi)
Definition: pentagonal.cpp:55