Loading [MathJax]/extensions/tex2jax.js
Parallel numerical verification of the σ_odd problem  October 6, 2018
All Classes Namespaces Files Functions Variables Typedefs Macros
helper.cpp
Go to the documentation of this file.
1 /* -*- coding: latin-1 -*- */
2 /** \file common/sigmaodd/helper.cpp (January 17, 2018)
3  *
4  * GPLv3 --- Copyright (C) 2017, 2018 Olivier Pirson
5  * http://www.opimedia.be/
6  */
7 
8 // \cond
9 #include <cassert>
10 #include <cmath>
11 
12 #include <algorithm>
13 #include <fstream>
14 // \endcond
15 
16 #include "../helper/helper.hpp"
17 #include "helper.hpp"
18 
19 
20 namespace sigmaodd {
21 
22  /* ***********
23  * Functions *
24  *************/
25 
26  nat_type
28  if (n >= 17878103347812890625ul) { // >= (2^(64/8) - 1)^8 = 17878103347812890625
29  return 255; // = 2^(64/8) - 1
30  }
31  else {
32  nat_type root
33  = static_cast<nat_type>(std::floor(std::sqrt(std::sqrt(std::sqrt(static_cast<double>(n))))));
34 
35  // Correct possible rounding error due to floating point computation
36  while (square(square(square(root))) <= n) { // get the first value too big
37  ++root;
38  }
39  do { // get the correct value
40  --root;
41  } while (square(square(square(root))) > n);
42 
43  assert(square(square(square(root))) <= n);
44  assert(n < square(square(square(root + 1))));
45 
46  return root;
47  }
48  }
49 
50 
51  nat_type
53  if (n >= 18445618199572250625ul) { // >= (2^(64/4) - 1)^4 = 18445618199572250625
54  return 65535; // = 2^(64/4) - 1
55  }
56  else {
57  nat_type root = static_cast<nat_type>(std::floor(std::sqrt(std::sqrt(static_cast<double>(n)))));
58 
59  // Correct possible rounding error due to floating point computation
60  while (square(square(root)) <= n) { // get the first value too big
61  ++root;
62  }
63  do { // get the correct value
64  --root;
65  } while (square(square(root)) > n);
66 
67  assert(square(square(root)) <= n);
68  assert(n < square(square(root + 1)));
69 
70  return root;
71  }
72  }
73 
74 
75  nat_type
77  if (n >= 18446744065119617025ul) { // >= (2^(64/2) - 1)^2 = 18446744065119617025
78  return 4294967295; // = 2^(64/2) - 1
79  }
80  else {
81  nat_type sqrt_n = static_cast<nat_type>(floor(sqrt(static_cast<double>(n))));
82 
83  /* Correct possible rounding error due to floating point computation */
84  while (square(sqrt_n) <= n) { /* get the first value too big */
85  ++sqrt_n;
86  }
87  do { /* get the correct value */
88  --sqrt_n;
89  } while (square(sqrt_n) > n);
90 
91  assert(square(sqrt_n) <= n);
92  assert(n < square(sqrt_n + 1));
93 
94  return sqrt_n;
95  }
96  }
97 
98 
99  std::vector<nat_type>
100  load_bad_table(const std::string &filename,
101  const std::vector<nat_type> &bad_table) {
102  std::vector<nat_type> new_bad_table = load_numbers(filename);
103 
104  if (!new_bad_table.empty()) {
105  new_bad_table.insert(new_bad_table.begin(), bad_table.cbegin(), bad_table.cend());
106  }
107 
108  sort(new_bad_table.begin(), new_bad_table.end());
109 
110  return new_bad_table;
111  }
112 
113 
114  std::vector<nat_type>
115  load_numbers(const std::string &filename) {
116  std::vector<nat_type> list;
117  std::ifstream infile(filename, std::ios::in);
118 
119  if (infile.is_open()) {
120  std::string line;
121 
122  while (getline(infile, line)) {
123  const auto i = line.find_first_not_of("0123456789");
124  const std::string s(i == std::string::npos
125  ? line
126  : (i > 0
127  ? line.substr(0, i)
128  : ""));
129 
130  if (!s.empty()) {
131  const nat_type n = std::stoul(s);
132 
133  list.push_back(n);
134  }
135  }
136 
137  infile.close();
138  }
139 
140  return list;
141  }
142 } // namespace sigmaodd
unsigned long nat_type
Type for natural number used in all code, on 64 bits.
Definition: sigmaodd.cl:22
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 floor_eighth_root(nat_type n)
Return the eighth root of n rounded to below.
Definition: helper.cpp:27
A lot of functions and stuffs to deal the sigma_odd problem and related stuffs.
Definition: divisors.cpp:22
nat_type floor_fourth_root(nat_type n)
Return the fourth root of n rounded to below.
Definition: helper.cpp:52
Some generic helper functions for programs.
std::vector< nat_type > load_numbers(const std::string &filename)
Read the file and extract each natural number begining a line. Return the list of these numbers...
Definition: helper.cpp:115
constexpr double square(double x)
Return x*x.
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