severalgos/3np1 mod problem C++  June 21, 2020
lib_3np1_mod.hpp
Go to the documentation of this file.
1 /* -*- coding: latin-1 -*- */
2 
3 /** \mainpage
4  * Module lib_3np1_mod (February 29, 2016)
5  *
6  * The C++ program *prog_3np1_mod* check
7  * the dynamic of
8  * **3n+1 function in a modulo arithmetic**.
9  *
10  * Results:
11  * \verbinclude exceptions_8.txt
12  * \verbinclude exceptions_16.txt
13  * \verbinclude exceptions_24.txt
14  * \verbinclude exceptions_32.txt
15  *
16  * See
17 \htmlonly
18 <ul>
19 <li>
20  Complete <strong>C++ sources</strong> on
21  <strong><a href="https://bitbucket.org/OPiMedia/severalgos/src/master/3np1_mod_problem/">Bitbucket</a></strong>
22 </li>
23 <li>
24  This
25  <strong><a href="http://www.opimedia.be/DS/online-documentations/severalgos/3np1-mod-problem-cpp/html/">C++ HTML online documentation</a></strong>
26 </li>
27 </ul>
28 \endhtmlonly
29  *
30  * Piece of severalgos.
31  * https://bitbucket.org/OPiMedia/severalgos
32  *
33  * GPLv3 --- Copyright (C) 2015, 2016 Olivier Pirson
34  * http://www.opimedia.be/
35  */
36 
37 /** \file lib_3np1_mod.hpp
38  * \brief Function definitions.
39  */
40 
41 #include <cstdint>
42 
43 #include <set>
44 #include <vector>
45 
46 
47 
48 #ifndef LIB_3NP1_MOD__LIB_3NP1_MOD__H_
49 #define LIB_3NP1_MOD__LIB_3NP1_MOD__H_
50 
51 namespace lib_3np1_mod {
52 
53  /* *******
54  * Types *
55  *********/
56  typedef uint64_t value_type;
57 
59 
61 
62 
63 
64  /* *********************
65  * Function prototypes *
66  ***********************/
67  /** \brief
68  * Collatz C function.
69  *
70  * \f$C(n) = \frac{n}{2}\f$ if n even\n
71  * \f$C(n) = 3n + 1\f$ if n odd
72  *
73  * http://oeis.org/A006370
74  *
75  * \param n <= (numeric_limits<value_type>::max() - 1)/3
76  */
77  inline
78  value_type
79  C(value_type n);
80 
81 
82  /** \brief
83  * Return
84  * \f$C(n) = \frac{n}{2}\f$ if n even,\n
85  * \f$C(n) = (3n + 1) \& mask\f$ if n odd
86  *
87  * \param n <= mask
88  * \param mask
89  */
90  inline
91  value_type
92  C_mask(value_type n, value_type mask);
93 
94 
95  /** \brief
96  * \f$m_2\f$ function.
97  *
98  * Exponent of highest power of 2 dividing n
99  * = number of beginning 0 bits of n
100  * = 2-adic valuation of n.
101  *
102  * http://oeis.org/A007814
103  *
104  * \param n != 0
105  */
106  inline
107  value_type
108  m2(value_type n);
109 
110 
111  /** \brief
112  * F odd function.
113  *
114  * \f$F(0) = 0\f$\n
115  * \f$F(n) = \frac{n}{2^k}\f$ if n even\n
116  * \f$F(n) = \frac{3n + 1}{2^k}\f$ if n odd\n
117  * which k as large as possible = \f$m_2(...)\f$.
118  *
119  * http://oeis.org/A139391
120  *
121  * \param n <= (numeric_limits<value_type>::max() - 1)/3
122  */
123  inline
124  value_type
125  F(value_type n);
126 
127 
128  /** \brief
129  * Return\n
130  * \f$F(0) = 0\f$\n
131  * \f$F(n) = \frac{n}{2^k}\f$ if n even\n
132  * \f$F(n) = \frac{(3n + 1) \& mask}{2^k}\f$ if n odd\n
133  * which k as large as possible = \f$m_2(...)\f$.
134  *
135  * \param n <= mask
136  * \param mask
137  */
138  inline
139  value_type
140  F_mask(value_type n, value_type mask);
141 
142 
143  /** \brief
144  * Calculate and return
145  * all null and odd exceptions
146  * for the 3n + 1 problem
147  * with modulo (mask + 1).
148  *
149  * \param mask
150  */
151  std::set<value_type>
152  search_mask_exceptions(value_type mask);
153 
154 
155  /** \brief
156  * Calculate and return
157  * all null and odd exceptions
158  * for the 3n + 1 problem
159  * with modulo \f$2^32\f$.
160  */
161  std::set<value_type>
163 
164 
165  /** \brief
166  * Calculate and return
167  * all null and odd exceptions
168  * for the 3n + 1 problem
169  * with modulo \f$2^32\f$.
170  */
171  std::set<value_type>
173 
174 
175  /** \brief
176  * Given operation,
177  * return the start of path
178  * from n to the first value < n or to the first repeated value.
179  *
180  * \param n <= mask
181  * \param operation_mask must be C_mask, F_mask or T_mask
182  * \param mask
183  */
184  std::vector<value_type>
185  start_path_mask(value_type n,
186  operation_mask_type operation_mask, value_type mask);
187 
188 
189  /** \brief
190  * Terras T function.
191  *
192  * \f$T(n) = \frac{n}{2}\f$ if n even\n
193  * \f$T(n) = \frac{3n + 1}{2}\f$ if n odd
194  *
195  * http://oeis.org/A014682
196  *
197  * \param n <= (numeric_limits<value_type>::max() - 1)/3
198  */
199  inline
200  value_type
201  T(value_type n);
202 
203 
204  /** \brief
205  * Return\n
206  * \f$T(n) = \frac{n}{2}\f$ if n even\n
207  * \f$T(n) = \frac{(3n + 1) \& mask}{2}\f$ if n odd
208  *
209  * \param n <= mask
210  * \param mask
211  */
212  inline
213  value_type
214  T_mask(value_type n, value_type mask);
215 
216 } // namespace lib_3np1_mod
217 
218 
219 #include "lib_3np1_mod__inline.hpp"
220 
221 #endif // LIB_3NP1_MOD__LIB_3NP1_MOD_H_
value_type m2(value_type n)
function.
value_type C_mask(value_type n, value_type mask)
Return if n even, if n odd.
value_type F(value_type n)
F odd function.
std::vector< value_type > start_path_mask(value_type n, operation_mask_type operation_mask, value_type mask)
Given operation, return the start of path from n to the first value < n or to the first repeated valu...
std::set< value_type > search_mask_exceptions_32()
Calculate and return all null and odd exceptions for the 3n + 1 problem with modulo ...
value_type F_mask(value_type n, value_type mask)
Return if n even if n odd which k as large as possible = .
value_type(* operation_type)(value_type)
value_type(* operation_mask_type)(value_type, value_type)
value_type C(value_type n)
Collatz C function.
std::set< value_type > search_mask_exceptions_64()
Calculate and return all null and odd exceptions for the 3n + 1 problem with modulo ...
std::set< value_type > search_mask_exceptions(value_type mask)
Calculate and return all null and odd exceptions for the 3n + 1 problem with modulo (mask + 1)...
value_type T(value_type n)
Terras T function.
value_type T_mask(value_type n, value_type mask)
Return if n even if n odd.
uint64_t value_type