Loading [MathJax]/extensions/tex2jax.js
Parallel numerical verification of the σ_odd problem  October 6, 2018
All Classes Namespaces Files Functions Variables Typedefs Macros
check_square.cpp
Go to the documentation of this file.
1 /* -*- coding: latin-1 -*- */
2 /** \file sequential/check_square.cpp (January 6, 2018)
3  * \brief
4  * Check odd square numbers for the varsigma_odd problem
5  * and print some numbers to show progression.
6  *
7  * GPLv3 --- Copyright (C) 2017, 2018 Olivier Pirson
8  * http://www.opimedia.be/
9  */
10 
11 // \cond
12 #include <cstdlib>
13 
14 #include <chrono>
15 #include <iostream>
16 // \endcond
17 
18 #include "../common/helper/helper.hpp"
20 
21 
22 
23 /* ***********
24  * Prototype *
25  *************/
26 
27 void
29 
30 
31 
32 /* **********
33  * Function *
34  ************/
35 
36 void
38  std::cerr << "Usage: check_square [options]" << std::endl
39  << std::endl
40  << "Options:" << std::endl
41  << " --complete check complete path until 1 (by default check only partial path until < n)" << std::endl
42  << " --first n square of first odd number to check (9 by default)" << std::endl
43  << " --last n square of last odd number to check (10000000000 by default)" << std::endl
44  << " --print print partial or complete path for each number checked (by default only some numbers are printed to show progression)" << std::endl;
45 
46  exit(EXIT_FAILURE);
47 }
48 
49 
50 
51 /* ******
52  * Main *
53  ********/
54 
55 int
56 main(int argc, const char* const argv[]) {
57  // Load primes table
59  std::cerr << "! Impossible to load \"" << sigmaodd::prime_filename << '"' << std::endl
60  << std::endl;
61  help_and_exit();
62  }
63 
64 
65  sigmaodd::nat_type first = 9;
66  sigmaodd::nat_type last = 10000000000; // = 10^10 ~= 2^33
67  bool complete = false;
68  bool print = false;
69 
70  // Read command line parameters
71  for (unsigned int i = 1; i < static_cast<unsigned int>(argc); ++i) {
72  const std::string param(argv[i]);
73 
74  if (param == "--complete") {
75  complete = true;
76  }
77  else if (param == "--first") {
78  first = std::max(9ul, helper::get_ulong(argc, argv, ++i, &help_and_exit));
79  if (sigmaodd::is_even(first)) {
80  ++first;
81  }
82  }
83  else if (param == "--last") {
84  last = helper::get_ulong(argc, argv, ++i, &help_and_exit);
85  if (sigmaodd::is_even(last)) {
86  --last;
87  }
88  }
89  else if (param == "--print") {
90  print = true;
91  }
92  else {
93  help_and_exit();
94  }
95  }
96 
97  const sigmaodd::nat_type sqrt_first
99  ? sigmaodd::ceil_square_root(first) + 1
100  : sigmaodd::ceil_square_root(first));
101  const sigmaodd::nat_type sqrt_last
103  ? sigmaodd::floor_square_root(last) - 1
105 
106 
107  // Print intern configuration
109 
110  // Print parameters
111  std::cout << "sequential/check_square"
112  << "\tFirst: " << first
113  << "\tLast: " << last
114  << "\tSqrt first: " << sqrt_first
115  << "\tSqrt last: " << sqrt_last
116  << "\tComplete?: " << helper::to_string(complete)
117  << "\tPrint?: " << helper::to_string(print) << std::endl;
118 
119  // Print table legend
120  std::cout << std::endl
121  << "i\ti^2\tLower\tPartial length\tPartial path";
122  if (complete) {
123  std::cout << "\tLength\tPath";
124  }
125  std::cout << std::endl;
126  std::cout.flush();
127 
128 
129  // Main calculation
130  const std::chrono::steady_clock::time_point clock_start = std::chrono::steady_clock::now();
131 
132  // Print always the first tried
133  {
134  const sigmaodd::nat_type n = sigmaodd::square(sqrt_first);
135 
136  std::cout << sqrt_first << '\t';
137  if (complete) {
139  }
140  else {
142  }
143  }
144 
145  // Check next numbers but print all only if option enabled
146  for (sigmaodd::nat_type i = sqrt_first + 2; i <= sqrt_last; i += 2) {
148 
149  const bool print_this = print || ((i & 0xFFF) == 1);
150  if (print_this) {
151  std::cout << i << '\t';
152  }
153 
154  if (complete) {
156  }
157  else {
159  }
160  }
161 
162  // End
163  std::chrono::duration<double> duration = std::chrono::steady_clock::now() - clock_start;
164 
165  std::cout << "Total duration: " << helper::duration_to_string(duration)
166  << std::endl;
167 
168  return EXIT_SUCCESS;
169 }
void sequential_check_varsigma_odd_perfect_square(nat_type n, bool print, bool print_lower, bool print_length, bool print_path)
Return sequential_check_varsigma_odd(), but only for n perfect square.
Definition: sequential.cpp:124
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.
int main(int argc, const char *const argv[])
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
void help_and_exit()
void sequential_check_varsigma_odd_perfect_square_complete(nat_type n, bool print, bool print_lower, bool print_length, bool print_path)
Return sequential_check_varsigma_odd_complete(), but only for n perfect square.
Definition: sequential.cpp:179
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
constexpr double square(double x)
Return x*x.
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