Loading [MathJax]/extensions/tex2jax.js
Parallel numerical verification of the σ_odd problem  October 6, 2018
All Classes Namespaces Files Functions Variables Typedefs Macros
primes.hpp
Go to the documentation of this file.
1 /* -*- coding: latin-1 -*- */
2 /** \file common/sigmaodd/primes.hpp (January 5, 2018)
3  * \brief
4  * Functions to access to tables of precaculated prime numbers
5  * and offsets to iterate on possible prime numbers.
6  *
7  * By default a table of all primes < 2^28 is used.
8  * This table must be read from the binary file big_data/prime28.bin.
9  *
10  * If the macro PRIME16 is defined
11  * then a little table of all primes < 2^16 is used.
12  * This table is automatically included by the compilation.
13  *
14  * If the macro PRIME16 is defined
15  * then there are also a table of offset to iterate on possible primes.
16  *
17  * GPLv3 --- Copyright (C) 2017, 2018 Olivier Pirson
18  * http://www.opimedia.be/
19  */
20 
21 #ifndef PROGS_SRC_COMMON_SIGMAODD_PRIMES_HPP_
22 #define PROGS_SRC_COMMON_SIGMAODD_PRIMES_HPP_
23 
24 // \cond
25 #include <cstdint>
26 
27 #include <string>
28 #include <vector>
29 // \endcond
30 
31 #include "divisors.hpp"
32 
33 
34 
35 // If defined the table of primes contains all odd prime numbers < 2^16 instead < 2^28.
36 // #define PRIME16
37 
38 
39 
40 namespace sigmaodd {
41 
42  /* ******
43  * Type *
44  ********/
45 
46  /** \brief
47  * Type for prime number, particularly for the table of primes.
48  */
49  typedef uint32_t prime_type;
50 
51 
52 
53  /* ***********
54  * Constants *
55  *************/
56 
57 #ifdef PRIME16
58  /** \brief
59  * Modulo used for offsets to jump to one potential prime number to next.
60  */
61  constexpr nat_type array_potential_prime_offsets_modulo_ = 210; // = 2*3*5*7
62 #endif
63 
64 
65 #ifdef PRIME16
66  /** \brief
67  * Number of offset values in the table array_potential_prime_offsets_.
68  */
69  constexpr unsigned int array_potential_prime_offsets_nb_ = 48;
70 #endif
71 
72 
73  /** \brief
74  * Number of *odd* prime numbers in the table array_odd_primes_.
75  *
76  * https://primes.utm.edu/nthprime/
77  * 14630841th prime (with 2): 268435361
78  * 14630842th prime (with 2): 268435367
79  * 14630843th prime (with 2): 268435399 < 2^28
80  * 14630844th prime (with 2): 268435459 > 2^28
81  */
82 #ifdef PRIME16
83  constexpr unsigned int array_odd_primes_nb_ = 6542 - 1; // all odd primes < 2^16
84 #else
85  constexpr unsigned int array_odd_primes_nb_ = 14630843u - 1; // all odd primes < 2^28
86  // constexpr unsigned int array_odd_primes_nb_ = 203280221u - 1; // all odd primes < 2^32
87 #endif
88 
89 
90 #ifdef PRIME16
91  /** \brief
92  * Array of offsets to jump to one potential prime number to next.
93  * See potential_prime_offsets_table_by_index().
94  */
95  extern const nat_type array_potential_prime_offsets_[array_potential_prime_offsets_nb_];
96 #endif
97 
98 
99  /** \brief
100  * Default filename for the binary file "big_data/prime28.bin".
101  */
102  extern const std::string prime_filename;
103 
104 
105 
106  /* **********
107  * Variable *
108  ************/
109  /** \brief
110  * Array of all *odd* prime numbers < 2^28 with a final 0.
111  * (Or < 2^16 if the macro PRIME16 is defined.)
112  */
113 #ifdef PRIME16
114  extern const prime_type array_odd_primes_[array_odd_primes_nb_ + 1];
115 #else
116  extern prime_type* array_odd_primes_;
117 #endif
118 
119 
120 
121  /* ************
122  * Prototypes *
123  **************/
124 
125  /** \brief
126  * Return a list of prime factors with their exponents.
127  *
128  * @param n != 0 <= MAX_POSSIBLE_N
129  */
130  std::vector<FactorExp>
131  factorize(nat_type n);
132 
133 
134  /** \brief
135  * Return the number corresponding to the factorization.
136  *
137  * @param prime_exps list of primes numbers with their exponent such that the corresponding number is not too big for nat_type
138  */
139  nat_type
140  factorization_to_n(std::vector<FactorExp> prime_exps);
141 
142 
143  /** \brief
144  * Return the number of all divisors of the number corresponding to the factorization.
145  *
146  * @param prime_exps list of distinct primes numbers with their exponent such that the corresponding number is not too big for nat_type
147  */
148  nat_type
149  factorization_to_nu(std::vector<FactorExp> prime_exps);
150 
151 
152  /** \brief
153  * Return the number of odd divisors of the number corresponding to the factorization.
154  *
155  * @param prime_exps list of distinct primes numbers with their exponent such that the corresponding number is not too big for nat_type
156  */
157  nat_type
158  factorization_to_nu_odd(std::vector<FactorExp> prime_exps);
159 
160 
161  /** \brief
162  * Return the sum of all divisors of the number corresponding to the factorization.
163  *
164  * @param prime_exps list of distinct primes numbers with their exponent such that the corresponding number is not too big for nat_type
165  */
166  nat_type
167  factorization_to_sigma(std::vector<FactorExp> prime_exps);
168 
169 
170  /** \brief
171  * Return the sum of odd divisors of the number corresponding to the factorization.
172  *
173  * @param prime_exps list of distinct primes numbers with their exponent such that the corresponding number is not too big for nat_type
174  */
175  nat_type
176  factorization_to_sigma_odd(std::vector<FactorExp> prime_exps);
177 
178 
179  /** \brief
180  * Return true iff n is a prime number
181  */
182  bool
183  is_prime(nat_type n);
184 
185 
186  /** \brief
187  * Return true iff n is a prime number present in the precalculated table.
188  */
189  bool
191 
192 
193  /** \brief
194  * Return the (i + 1)th odd prime number from the precalculated table.
195  *
196  * @param i 0 <= i < odd_primes_table_nb()
197  *
198  * @return the item of index i of the odd primes table
199  */
200  inline
201  prime_type
202  odd_primes_table_by_index(unsigned int i);
203 
204 
205  /** \brief
206  * Return the last odd prime number in the precalculated table.
207  */
208  inline
209  prime_type
211 
212 
213  /** \brief
214  * Return the number of odd prime numbers in the precalculated table.
215  */
216  constexpr
217  unsigned int
219 
220 
221  /** \brief
222  * Return a pointer to the first number in the precalculated table.
223  */
224  inline
225  const prime_type*
227 
228 
229 #ifdef PRIME16
230  /** \brief
231  * Return the item of index i of the potential prime offsets table.
232  *
233  * @param i 0 <= i < potential_prime_offsets_table_nb
234  *
235  * @return the item of index i of the potential prime offsets table
236  */
237  inline
238  nat_type
239  potential_prime_offsets_table_by_index(unsigned int i);
240 #endif
241 
242 
243 #ifdef PRIME16
244  /** \brief
245  * Return the modulo used by the potential prime offsets table
246  */
247  constexpr
248  nat_type
249  potential_prime_offsets_table_modulo();
250 #endif
251 
252 
253 #ifdef PRIME16
254  /** \brief
255  * Return the number of offsets in the potential prime offsets table
256  */
257  constexpr
258  unsigned int
259  potential_prime_offsets_table_nb();
260 #endif
261 
262 
263  /** \brief
264  * Read the binary file prime_filename
265  * to fill the table with all primes < 2^28.
266  * This table must be read from the binary file big_data/prime28.bin.
267  *
268  * If prime_filename is not available in the same directory
269  * then search it in parents directories.
270  *
271  * If the macro PRIME16 is defined
272  * the this function do nothing.
273  *
274  * @return true iff the file was completely loaded
275  */
276  bool
278 
279 } // namespace sigmaodd
280 
281 #include "primes__inline.hpp"
282 
283 #endif // PROGS_SRC_COMMON_SIGMAODD_PRIMES_HPP_
uint64_t nat_type
Type for natural number used in all code, on 64 bits.
Definition: helper.hpp:33
prime_type odd_primes_table_last()
Return the last odd prime number in the precalculated table.
nat_type factorization_to_nu_odd(std::vector< FactorExp > prime_exps)
Return the number of odd divisors of the number corresponding to the factorization.
Definition: primes.cpp:129
const std::string prime_filename
Default filename for the binary file "big_data/prime28.bin".
Definition: primes.cpp:35
bool is_prime(nat_type n)
Return true iff n is a prime number.
Definition: primes.cpp:169
uint32_t prime_type
Type for prime number, particularly for the table of primes.
Definition: primes.hpp:49
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
A lot of functions and stuffs to deal the sigma_odd problem and related stuffs.
Definition: divisors.cpp:22
bool is_prime_in_odd_primes_table(nat_type n)
Return true iff n is a prime number present in the precalculated table.
Definition: primes.cpp:215
const prime_type * odd_primes_table_ptr()
Return a pointer to the first number in the precalculated table.
std::vector< FactorExp > factorize(nat_type n)
Return a list of prime factors with their exponents.
Definition: primes.cpp:58
nat_type factorization_to_n(std::vector< FactorExp > prime_exps)
Return the number corresponding to the factorization.
Definition: primes.cpp:105
prime_type odd_primes_table_by_index(unsigned int i)
Return the (i + 1)th odd prime number from the precalculated table.
nat_type factorization_to_sigma_odd(std::vector< FactorExp > prime_exps)
Return the sum of odd divisors of the number corresponding to the factorization.
Definition: primes.cpp:155
nat_type factorization_to_sigma(std::vector< FactorExp > prime_exps)
Return the sum of all divisors of the number corresponding to the factorization.
Definition: primes.cpp:143
constexpr unsigned int array_odd_primes_nb_
Number of odd prime numbers in the table array_odd_primes_.
Definition: primes.hpp:85
constexpr unsigned int odd_primes_table_nb()
Return the number of odd prime numbers in the precalculated table.
nat_type factorization_to_nu(std::vector< FactorExp > prime_exps)
Return the number of all divisors of the number corresponding to the factorization.
Definition: primes.cpp:117
Functions in link with divisor notion: sum of divisors, factorization, GCD, coprime, ...
prime_type * array_odd_primes_
Array of all odd prime numbers < 2^28 with a final 0. (Or < 2^16 if the macro PRIME16 is defined...
Definition: primes.cpp:48