Loading [MathJax]/extensions/tex2jax.js
Parallel numerical verification of the σ_odd problem  October 6, 2018
All Classes Namespaces Files Functions Variables Typedefs Macros
varsigmaoddprob.cpp
Go to the documentation of this file.
1 /* -*- coding: latin-1 -*- */
2 /** \file common/varsigmaoddprob.cpp (January 5, 2018)
3  * \brief
4  * The varsigma_odd problem.
5  *
6  * GPLv3 --- Copyright (C) 2017, 2018 Olivier Pirson
7  * http://www.opimedia.be/
8  */
9 
10 // \cond
11 #include <cstdlib>
12 
13 #include <chrono>
14 #include <iostream>
15 // \endcond
16 
17 #include "helper/helper.hpp"
18 #include "sigmaodd/dot.hpp"
19 #include "sigmaodd/primes.hpp"
20 #include "sigmaodd/sigmaodd.hpp"
21 
22 
23 
24 /* ***********
25  * Prototype *
26  *************/
27 
28 void
30 
31 
32 
33 /* **********
34  * Function *
35  ************/
36 
37 void
39  std::cerr << "Usage: varsigmaoddprob [options]" << std::endl
40  << std::endl
41  << "Method options to calculate sum of divisors:" << std::endl
42  << " --bound factorize method with bound shortcuts (by default)" << std::endl
43  << " --factorize factorize number with partial prime numbers table" << std::endl
44  << " --naive try each odd possible divisor)" << std::endl
45  << std::endl
46  << "Options:" << std::endl
47  << " --dot print DOT graph instead normal data" << std::endl
48  << " --dot-circular print DOT graph instead normal data" << std::endl
49  << " --first n first odd number to check (3 by default)" << std::endl
50  << " --last n last odd number to check (101 by default)" << std::endl
51  << " --nb n number of odd numbers to check" << std::endl
52  << " --only-longer print number with length greater than 1 (not for all options)" << std::endl
53  << " --path print path for each number checked" << std::endl
54  << " --print-long print all number that require more than 1 iteration" << std::endl
55  << " --print-sqr print all square numbers" << std::endl
56  << " --graph-point print points instead nodes (only for graph)" << std::endl
57  << " --graph-shortcut print shortcut graph (only for graph)" << std::endl;
58 
59  exit(EXIT_FAILURE);
60 }
61 
62 
63 
64 /* ******
65  * Main *
66  ********/
67 
68 int
69 main(int argc, const char* const argv[]) {
70  // Load primes table
72  std::cerr << "! Impossible to load \"" << sigmaodd::prime_filename << '"' << std::endl
73  << std::endl;
74  help_and_exit();
75  }
76 
77 
78  enum Methods: unsigned int {factorize, factorize_bound, naive, print_long, print_sqr};
79 
80  const std::string method_strings[5] = {"factorize",
81  "factorize bound",
82  "naive",
83  "print-long",
84  "print-sqr"};
85 
86  sigmaodd::nat_type first = 3;
87  sigmaodd::nat_type last = 101;
88 
89  bool dot_circular = false;
90  bool graph_point = false;
91  bool graph_shortcut = false;
92  unsigned int method = factorize;
93  bool print_dot = false;
94  bool print_only_longer = false;
95  bool print_path = false;
96 
97 
98  // Read command line parameters
99  for (unsigned int i = 1; i < static_cast<unsigned int>(argc); ++i) {
100  const std::string param(argv[i]);
101 
102  if (param == "--dot") {
103  print_dot = true;
104  dot_circular = false;
105  }
106  else if (param == "--dot-circular") {
107  print_dot = true;
108  dot_circular = true;
109  }
110  else if (param == "--bound") {
111  method = factorize_bound;
112  }
113  else if (param == "--factorize") {
114  method = factorize;
115  }
116  else if (param == "--first") {
117  first = std::max(3ul, helper::get_ulong(argc, argv, ++i, &help_and_exit));
118  if (sigmaodd::is_even(first)) {
119  ++first;
120  }
121  }
122  else if (param == "--graph-point") {
123  graph_point = true;
124  }
125  else if (param == "--graph-shortcut") {
126  graph_shortcut = true;
127  }
128  else if (param == "--last") {
129  last = helper::get_ulong(argc, argv, ++i, &help_and_exit);
130  if (sigmaodd::is_even(last)) {
131  --last;
132  }
133  }
134  else if (param == "--naive") {
135  method = naive;
136  }
137  else if (param == "--nb") {
138  last = first + helper::get_ulong(argc, argv, ++i, &help_and_exit)*2 - 1;
139  }
140  else if (param == "--only-longer") {
141  print_only_longer = true;
142  }
143  else if (param == "--path") {
144  print_path = true;
145  }
146  else if (param == "--print-long") {
147  method = print_long;
148  }
149  else if (param == "--print-sqr") {
150  method = print_sqr;
151  }
152  else {
153  help_and_exit();
154  }
155  }
156 
157  const sigmaodd::nat_type nb = (first <= last
158  ? (last - first)/2 + 1
159  : 0);
160 
161  if (!print_dot) {
162  // Print intern configuration
164 
165  // Print parameters
166  std::cout << "First: " << first
167  << "\tLast: " << last
168  << "\tNb: " << nb;
169  if ((method != print_long) && (method != print_sqr)) {
170  std::cout << "\tPrint only longer?: " << helper::to_string(print_only_longer);
171  }
172  std::cout << "\tPrint path?: " << helper::to_string(print_path)
173  << "\tMethod: " << method_strings[method]
174  << std::endl;
175 
176  // Print table legend
177  std::cout << std::endl
178  << "n\tLower";
179  if ((method != print_long) && (method != print_sqr)) {
180  std::cout << "\tLength";
181  }
182  if (print_path) {
183  std::cout << "\tPath";
184  }
185  std::cout << std::endl;
186  std::cout.flush();
187  }
188 
189 
190  // Main calculation
191  const std::chrono::steady_clock::time_point clock_start = std::chrono::steady_clock::now();
192 
193  if (print_dot) {
194  sigmaodd::print_varsigmaodd_dot(first, last, !graph_point, graph_shortcut, dot_circular);
195  }
196  else if (method == factorize) {
197  sigmaodd::print_sigmaodd__factorize(first, last, print_only_longer, print_path);
198  }
199  else if (method == factorize_bound) {
200  sigmaodd::print_sigmaodd__factorize_bound(first, last, print_only_longer, print_path);
201  }
202  else if (method == naive) {
203  sigmaodd::print_sigmaodd__naive(first, last, print_only_longer, print_path);
204  }
205  else if (method == print_long) {
206  sigmaodd::print_long(first, last, print_path);
207  }
208  else if (method == print_sqr) {
209  sigmaodd::print_square(first, last, print_path);
210  }
211  else {
212  exit(EXIT_FAILURE);
213  }
214  std::cout.flush();
215 
216 
217  // End
218  if (!print_dot) {
219  std::chrono::duration<double> duration = std::chrono::steady_clock::now() - clock_start;
220 
221  std::cout << "Total duration: " << helper::duration_to_string(duration)
222  << std::endl;
223  }
224 
225  return EXIT_SUCCESS;
226 }
uint64_t nat_type
Type for natural number used in all code, on 64 bits.
Definition: helper.hpp:33
void help_and_exit()
const std::string prime_filename
Default filename for the binary file "big_data/prime28.bin".
Definition: primes.cpp:35
void print_sigmaodd__naive(nat_type first, nat_type last, bool print_only_longer, bool print_path)
Iterate from first to last (included) and print result of the sum_odd_divisors_divided_until_odd_iter...
Definition: sigmaodd.cpp:407
constexpr bool is_even(nat_type n)
Return true iff n is even.
void print_sigmaodd__factorize_bound(nat_type first, nat_type last, bool print_only_longer, bool print_path)
Iterate from first to last (included) and print result of the sum_odd_divisors_divided_until_odd_iter...
Definition: sigmaodd.cpp:382
std::string duration_to_string(std::chrono::duration< double > duration_second)
Return a string with the duration expressed in milliseconds, seconds, minutes and hours...
Definition: helper.cpp:61
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
void print_square(nat_type first, nat_type last, bool print_path)
Definition: sigmaodd.cpp:432
To produce graph DOT file.
Functions to access to tables of precaculated prime numbers and offsets to iterate on possible prime ...
void print_varsigmaodd_dot(nat_type first, nat_type last, bool show_node, bool shortcut, bool circular)
Print to std::cout in a DOT format, a graph of path of the sigma odd problem.
Definition: dot.cpp:76
void print_long(nat_type first, nat_type last, bool print_path)
Iterate from first to last (included) and for each odd n not square such that is_little_mersenne_prim...
Definition: sigmaodd.cpp:213
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.
void print_sigmaodd__factorize(nat_type first, nat_type last, bool print_only_longer, bool print_path)
Iterate from first to last (included) and print result of the sum_odd_divisors_divided_until_odd_iter...
Definition: sigmaodd.cpp:357
void print_path(const std::vector< nat_type > &path, std::ostream &out)
Send to the stream a string representation of the path. All numbers are separated by the correspondin...
Definition: sigmaodd.cpp:251
void print_intern_config_compiler()
Print to stdcout the intern configuration of the compiler.
Definition: helper.cpp:148
Main functions to deal the sigma_odd problem.
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
std::string to_string(bool b)
Return the string "true" if b, else "false".
Definition: helper.cpp:177
int main(int argc, const char *const argv[])