Loading [MathJax]/extensions/tex2jax.js
Parallel numerical verification of the σ_odd problem  October 6, 2018
All Classes Namespaces Files Functions Variables Typedefs Macros
check_gentle_sequential.cpp
Go to the documentation of this file.
1 /* -*- coding: latin-1 -*- */
2 /** \file sequential/check_gentle_sequential.cpp (January 6, 2018)
3  * \brief
4  * Check odd gentle numbers for the varsigma_odd problem
5  * and print bad numbers.
6  *
7  * GPLv3 --- Copyright (C) 2017, 2018 Olivier Pirson
8  * http://www.opimedia.be/
9  */
10 
11 // \cond
12 #include <cstdlib>
13 
14 #include <algorithm>
15 #include <chrono>
16 #include <iostream>
17 // \endcond
18 
19 #include "../common/helper/helper.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: check_gentle [options] (sequential version)" << std::endl
40  << std::endl
41  << "Options:" << std::endl
42  << " --bad-table filename load bad numbers from this file" << std::endl
43  << " (If load bad numbers then must be contains all bad numbers < first." << std::endl
44  << " Can load them with several files.)" << std::endl
45  << " --first n first odd number to check (3 by default)" << std::endl
46  << " --last n last odd number to check (1000001 by default)" << std::endl
47  << " --nb n number of odd numbers to check" << std::endl
48  << " --no-print do not print bad numbers" << std::endl;
49 
50  exit(EXIT_FAILURE);
51 }
52 
53 
54 
55 /* ******
56  * Main *
57  ********/
58 
59 int
60 main(int argc, const char* const argv[]) {
61  // Load primes table
63  std::cerr << "! Impossible to load \"" << sigmaodd::prime_filename << '"' << std::endl
64  << std::endl;
65  help_and_exit();
66  }
67 
68 
69  std::vector<sigmaodd::nat_type> bad_table;
70  sigmaodd::nat_type first = 3;
71  sigmaodd::nat_type last = 1000001;
72  bool print_bad = true;
73 
74 
75  // Read command line parameters
76  for (unsigned int i = 1; i < static_cast<unsigned int>(argc); ++i) {
77  const std::string param(argv[i]);
78 
79  if (param == "--bad-table") {
80  std::string filename = helper::get_string(argc, argv, ++i, &help_and_exit);
81 
82  if (filename.empty() || !helper::is_file_exists(filename)) {
83  std::cerr << "! Failed reading file \"" << filename << '"' << std::endl;
84  help_and_exit();
85  }
86 
87  bad_table = sigmaodd::load_bad_table(filename, bad_table);
88  if (bad_table.empty()) {
89  std::cerr << "! Wrong format for the bad table file \"" << filename << '"' << std::endl;
90  help_and_exit();
91  }
92  }
93  else if (param == "--first") {
94  first = std::max(3ul, helper::get_ulong(argc, argv, ++i, &help_and_exit));
95  if (sigmaodd::is_even(first)) {
96  ++first;
97  }
98  }
99  else if (param == "--last") {
100  last = helper::get_ulong(argc, argv, ++i, &help_and_exit);
101  if (sigmaodd::is_even(last)) {
102  --last;
103  }
104  }
105  else if (param == "--nb") {
106  last = first + helper::get_ulong(argc, argv, ++i, &help_and_exit)*2 - 1;
107  }
108  else if (param == "--no-print") {
109  print_bad = false;
110  }
111  else {
112  help_and_exit();
113  }
114  }
115 
116  const std::set<sigmaodd::nat_type> bad_table_set(bad_table.cbegin(), bad_table.cend());
117 
118  bad_table.clear();
119 
120  const auto minmax = std::minmax_element(bad_table_set.cbegin(), bad_table_set.cend());
121 
122 
123  // Print intern configuration
125 
126  // Print parameters
127  std::cout << "sequential/check_gentle"
128  << "\tFirst: " << first
129  << "\tLast: " << last
130  << "\tNb: " << (first <= last
131  ? (last - first)/2 + 1
132  : 0);
133  if (!bad_table_set.empty()) {
134  std::cout << "\t# bad numbers loaded: " << bad_table_set.size()
135  << "\tBetween " << *minmax.first
136  << "\tand " << *minmax.second << std::endl;
137  }
138  std::cout << std::endl;
139 
140  // Print table legend
141  std::cout << std::endl
142  << 'n' << std::endl;
143  std::cout.flush();
144 
145 
146  // Main calculation
147  const std::chrono::steady_clock::time_point clock_start = std::chrono::steady_clock::now();
148 
149  if (bad_table_set.empty()) {
151  print_bad);
152  }
153  else {
155  bad_table_set,
156  0, std::max(first - 1, *minmax.second),
157  print_bad);
158  }
159 
160  // End
161  const std::chrono::duration<double> duration = std::chrono::steady_clock::now() - clock_start;
162 
163  std::cout << "Total duration: " << helper::duration_to_string(duration)
164  << std::endl;
165 
166  return EXIT_SUCCESS;
167 }
std::set< nat_type > sequential_check_gentle_varsigma_odd(nat_type first_n, nat_type last_n, bool print_bad)
Check in the order all odd gentle numbers between first_n and last_n, and if print_bad then print all...
Definition: sequential.cpp:29
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
constexpr bool is_even(nat_type n)
Return true iff n is even.
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
bool is_file_exists(std::string filename)
Return true iff the file (or directory) exists.
Definition: helper.cpp:140
std::string get_string(int argc, const char *const argv[], unsigned int i, void(*help_and_exit_function)())
Return argv[i] converted in string.
Definition: helper.cpp:92
int main(int argc, const char *const argv[])
Implementation of the sequential algorithms presented in the report. (Some functions are not use in t...
void print_intern_config_compiler()
Print to stdcout the intern configuration of the compiler.
Definition: helper.cpp:148
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::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