Loading [MathJax]/extensions/tex2jax.js
Parallel numerical verification of the σ_odd problem  October 6, 2018
All Classes Namespaces Files Functions Variables Typedefs Macros
table_harmonic.cpp
Go to the documentation of this file.
1 /* -*- coding: latin-1 -*- */
2 /** \file common/table_harmonic.cpp (January 5, 2018)
3  * \brief
4  * Print same information in the same format than
5  * ../../miscellaneous/harmonic/harmonic.py,
6  * to compare and check correction.
7  *
8  * GPLv3 --- Copyright (C) 2017, 2018 Olivier Pirson
9  * http://www.opimedia.be/
10  */
11 
12 // \cond
13 #include <cstdlib>
14 
15 #include <iomanip>
16 #include <iostream>
17 // \endcond
18 
19 #include "helper/helper.hpp"
20 #include "sigmaodd/harmonic.hpp"
21 #include "sigmaodd/divisors.hpp"
22 #include "sigmaodd/primes.hpp"
23 #include "sigmaodd/sigmaodd.hpp"
24 
25 
26 
27 /* ***********
28  * Prototype *
29  *************/
30 
31 void
33 
34 
35 
36 /* **********
37  * Function *
38  ************/
39 
40 void
42  std::cerr << "Usage: table_harmonic [options]" << std::endl
43  << std::endl
44  << "Method options to calculate sum of divisors:" << std::endl
45  << " --bound factorize method with bound shortcuts (by default)" << std::endl
46  << " --factorize factorize number with partial prime numbers table" << std::endl
47  << " --naive try each odd possible divisor)" << std::endl
48  << std::endl
49  << "Options:" << std::endl
50  << " --first n first number to check (1 by default)" << std::endl
51  << " --last n last number to check (50 by default)" << std::endl
52  << " --nb n number of numbers to check" << std::endl;
53 
54  exit(EXIT_FAILURE);
55 }
56 
57 
58 
59 /* ******
60  * Main *
61  ********/
62 
63 int
64 main(int argc, const char* const argv[]) {
65  // Load primes table
67  std::cerr << "! Impossible to load \"" << sigmaodd::prime_filename << '"' << std::endl
68  << std::endl;
69  help_and_exit();
70  }
71 
72 
73  enum Methods: unsigned int {factorize, factorize_bound, naive};
74 
75  const std::string method_strings[3] = {"factorize",
76  "factorize bound",
77  "naive"};
78 
79  sigmaodd::nat_type first = 1;
80  sigmaodd::nat_type last = 50;
81  unsigned int method = factorize;
82 
83 
84  // Read command line parameters
85  for (unsigned int i = 1; i < static_cast<unsigned int>(argc); ++i) {
86  const std::string param(argv[i]);
87 
88  if (param == "--bound") {
89  method = factorize_bound;
90  }
91  else if (param == "--factorize") {
92  method = factorize;
93  }
94  else if (param == "--first") {
95  first = std::max(1ul, helper::get_ulong(argc, argv, ++i, &help_and_exit));
96  }
97  else if (param == "--last") {
98  last = helper::get_ulong(argc, argv, ++i, &help_and_exit);
99  }
100  else if (param == "--naive") {
101  method = naive;
102  }
103  else if (param == "--nb") {
104  last = first + helper::get_ulong(argc, argv, ++i, &help_and_exit) - 1;
105  }
106  else {
107  help_and_exit();
108  }
109  }
110 
111 
112  // Print parameters (to stderr)
113  std::cerr << "First: " << first
114  << "\tLast: " << last
115  << "\tNb: " << (first <= last
116  ? (last - first)/2 + 1
117  : 0)
118  << "\tMethod: " << method_strings[method]
119  << std::endl;
120 
121 
122  // Print table legend
123  std::cout << "n\tsqrt\t|s_odd\tbound\tGUB\tDeTemp.\t|sum_o.\t|Half bound\tdiff h.\tfl.harm._odd\tharmonic_odd"
124  << std::endl;
125 
126 
127  // Calculate and print data
128  std::cout << std::fixed << std::setprecision(10);
129 
130  for (sigmaodd::nat_type n = first; n <= last; ++n) {
132  const sigmaodd::nat_type sqrt_n1 = sigmaodd::floor_square_root(n - 1);
133 
134  sigmaodd::nat_type exact;
135 
136  if (method == factorize) {
138  }
139  else if (method == factorize_bound) {
140  exit(EXIT_FAILURE);
141  }
142  else if (method == naive) {
144  }
145  else {
146  exit(EXIT_FAILURE);
147  }
148 
149  const sigmaodd::nat_type bound
150  = (sigmaodd::sum_odd(sqrt_n) +
152  const sigmaodd::nat_type gub_bound
153  = sigmaodd::sum_odd(sqrt_n)
154  + static_cast<sigmaodd::nat_type>(std::floor(sigmaodd::harmonic_odd(sqrt_n1)
155  * static_cast<double>(n)));
157  const sigmaodd::nat_type sum_odd_n = sigmaodd::sum_odd(n);
158 
159  const double diff_half = (sqrt_n1 == 0
160  ? 0
162  const double h_odd = sigmaodd::harmonic_odd(n);
163 
164  std::cout << n << '\t'
165  << sqrt_n << '\t'
166  << exact << '\t'
167  << bound << '\t'
168  << gub_bound << '\t'
169  << bound_detemple << '\t'
170  << sum_odd_n << '\t'
172  << sigmaodd::sum_floor_n_harmonic_odd(n, n) << '\t'
173  << diff_half << '\t'
174  << h_odd
175  << std::endl;
176 
177  assert(exact <= bound);
178  assert(exact <= gub_bound);
179  assert(exact <= bound_detemple);
180 
181  assert(bound <= gub_bound);
182  assert(gub_bound <= bound_detemple);
183 
184  assert(exact <= sum_odd_n);
185  if ((n != 2) && (n != 4)) {
186  assert(bound <= sum_odd_n);
187  }
188  }
189 
190  return EXIT_SUCCESS;
191 }
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
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
const std::string prime_filename
Default filename for the binary file "big_data/prime28.bin".
Definition: primes.cpp:35
double diff_half_harmonic_upper_bound(nat_type a, nat_type b)
Return an upper bound of H_a - 1/2 H_b.
Definition: harmonic.cpp:42
bool read_primes_table()
Read the binary file prime_filename to fill the table with all primes < 2^28. This table must be read...
Definition: primes.cpp:241
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.
Definition: divisors.cpp:444
Function to calculate harmonic number H_n = 1 + 1/2 + 1/3 + 1/4 + ... + 1/n and some variants and upp...
std::vector< FactorExp > factorize(nat_type n)
Return a list of prime factors with their exponents.
Definition: primes.cpp:58
Some generic helper functions for programs.
nat_type sum_floor_n_harmonic_odd(nat_type n, nat_type to_n)
Return floor(n/1) + floor(n/3) + floor(n/5) + floor(n/7) + ... + (n/to_n or floor(1/(to_n-1))).
Definition: harmonic.cpp:153
int main(int argc, const char *const argv[])
Main functions to deal the sigma_odd problem.
void help_and_exit()
Functions in link with divisor notion: sum of divisors, factorization, GCD, coprime, ...
unsigned long get_ulong(int argc, const char *const argv[], unsigned int i, void(*help_and_exit_function)())
Return argv[i] converted in integer.
Definition: helper.cpp:124
constexpr double harmonic_odd(nat_type n)
Return 1/1 + 1/3 + 1/5 + 1/7 + ... + (1/n or 1/(n-1)).
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).
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