1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
//! A collection of functions relating to the Collatz conjecture

#![deny(missing_docs, unused_imports)]
#![warn(clippy::unwrap_used)]

mod traits;
pub use traits::*;

mod impl_nonzero {
    use crate::traits::*;
    use beetle_nonzero::{ranges::RangeNonZeroUnsigned, traits::*, NonZero};
    use num::{traits::Pow, One};

    impl<T: PrimUint> Rules for NonZero<T> {
        fn odd_rule(&self) -> Self {
            let value = *self;
            (value + value + value) + Self::one()
        }

        fn even_rule(&self) -> Self {
            let two = Self::one() + Self::one();
            *self / two
        }

        fn rules(&self) -> Self {
            if self.is_even() {
                self.even_rule()
            } else {
                self.odd_rule()
            }
        }

        fn rules_halve_odds(&self) -> Self {
            if self.is_odd() {
                self.odd_rule().even_rule()
            } else {
                self.even_rule()
            }
        }

        fn rules_remove_trailing_zeros(&self) -> Self {
            if self.is_odd() {
                self.odd_rule().without_trailing_zeros()
            } else {
                self.without_trailing_zeros()
            }
        }
    }

    impl<T: PrimUint> Steps for NonZero<T> {
        fn steps_to_one(&self) -> u64 {
            let mut n: NonZero<T> = *self;
            let mut steps = 0;

            while !n.is_one() {
                n = n.rules();
                steps += 1;
            }
            steps
        }

        fn steps_to_decrease(&self) -> u64 {
            if self.is_even() {
                return 1;
            }

            let mut n = *self;
            let mut steps = 0;
            // Apply rules once beforehand for a potential performance gain.
            n = n.odd_rule();
            steps += (n.trailing_zeros() as u64) + 1;
            n = n.without_trailing_zeros();

            let starting_value = *self;
            while n > starting_value {
                n = n.odd_rule();
                steps += (n.trailing_zeros() as u64) + 1;
                n = n.without_trailing_zeros();
            }

            steps
        }

        fn steps_to_one_for_even_number(&self) -> u64 {
            let steps_to_become_odd: u64 = self.get().trailing_zeros().into();
            let without_zeros = self.without_trailing_zeros();
            steps_to_become_odd + without_zeros.steps_to_one_for_odd_number()
        }

        fn steps_to_one_for_odd_number(&self) -> u64 {
            let mut steps = 0;
            let mut n = *self;
            while !n.is_one() {
                // Number is known to be odd here,
                // so apply odd rule,
                n = n.odd_rule();
                steps += 1;

                // After the odd rule is applied, the resulting number is always even,
                // so remove trailing zeros,
                // and count each trailing zeros as a step
                let tz = n.trailing_zeros() as u64;
                n = n.without_trailing_zeros();
                steps += tz;
            }
            steps
        }
    }

    impl<T: PrimUint> WithoutTrailingZeros for NonZero<T> {
        type R = Self;
        fn without_trailing_zeros(&self) -> Self::R {
            let zeros: u32 = self.trailing_zeros();
            let two = Self::one() + Self::one();
            let power_of_two = two.pow(zeros);
            *self / power_of_two
        }
    }

    impl<T: PrimUint> Bouncy for NonZero<T> {
        fn is_bouncy(&self) -> bool {
            let steps: u64 = self.steps_to_one();
            // let range: Range<T> = num::iter::range(T::zero(), value);
            let range = RangeNonZeroUnsigned::new(NonZero::one(), *self);
            for x in range {
                let x_steps: u64 = x.steps_to_one();
                if x_steps >= steps {
                    return false;
                }
            }
            true
        }
    }

    impl<T: PrimUint> Transformations for NonZero<T> {
        fn transformations_to_one(&self) -> Vec<Self> {
            let mut v: Vec<NonZero<T>> = Vec::new();
            let mut n: NonZero<T> = *self;
            v.push(n);
            while !n.is_one() {
                n = n.rules();
                v.push(n);
            }
            v
        }
    }
}

mod impl_nonzero_biguint {
    use crate::traits::*;
    use beetle_nonzero::{ranges::RangeNonZeroBigUint, NonZeroBigUint};
    use num::{traits::Pow, One};

    impl Rules for NonZeroBigUint {
        fn odd_rule(&self) -> Self {
            let value = self.clone();
            let one = Self::one();
            let three = one.clone() + one.clone() + one.clone();
            value * three + one
        }

        fn even_rule(&self) -> Self {
            let two = Self::one() + Self::one();
            self.clone() / two
        }

        fn rules(&self) -> Self {
            if self.is_even() {
                self.even_rule()
            } else {
                self.odd_rule()
            }
        }

        fn rules_halve_odds(&self) -> Self {
            if self.is_odd() {
                self.odd_rule().even_rule()
            } else {
                self.even_rule()
            }
        }

        fn rules_remove_trailing_zeros(&self) -> Self {
            if self.is_odd() {
                self.odd_rule().without_trailing_zeros()
            } else {
                self.without_trailing_zeros()
            }
        }
    }

    impl Steps for NonZeroBigUint {
        fn steps_to_one(&self) -> u64 {
            let mut n = self.clone();
            let mut steps = 0;

            while !n.is_one() {
                n = n.rules();
                steps += 1;
            }
            steps
        }

        fn steps_to_decrease(&self) -> u64 {
            let mut n = self.clone();
            let mut steps = 0;
            let starting_value = self.clone();

            while n >= starting_value {
                n = n.rules();
                steps += 1;
            }

            steps
        }

        fn steps_to_one_for_even_number(&self) -> u64 {
            let steps_to_become_odd: u64 = self.trailing_zeros();
            let without_zeros = self.without_trailing_zeros();
            steps_to_become_odd + without_zeros.steps_to_one_for_odd_number()
        }

        fn steps_to_one_for_odd_number(&self) -> u64 {
            let mut n = self.clone();
            let mut steps = 0;
            while !self.is_one() {
                // Number is known to be odd here,
                // so apply odd rule,
                n = n.odd_rule();
                steps += 1;

                // After the odd rule is applied, the resulting number is always even,
                // so remove trailing zeros,
                // and count each trailing zeros as a step
                n = n.without_trailing_zeros();
                steps += n.trailing_zeros();
            }
            steps
        }
    }

    impl WithoutTrailingZeros for NonZeroBigUint {
        type R = Self;
        fn without_trailing_zeros(&self) -> Self::R {
            let mut n = self.clone();

            while !n.is_odd() {
                let zeros: u32 = self.trailing_zeros() as u32;
                let two = Self::one() + Self::one();
                let power_of_two = two.pow(zeros);
                n = self.clone() / power_of_two;
            }
            n
        }
    }

    impl Bouncy for NonZeroBigUint {
        fn is_bouncy(&self) -> bool {
            let steps: u64 = self.steps_to_one();
            // let range: Range<T> = num::iter::range(T::zero(), value);
            // let range = RangeNonZeroUnsigned::new(Self::one(), self.clone());
            let range = RangeNonZeroBigUint::new(Self::one(), self.clone());
            for x in range {
                let x_steps: u64 = x.steps_to_one();
                if x_steps >= steps {
                    return false;
                }
            }
            true
        }
    }

    impl Transformations for NonZeroBigUint {
        fn transformations_to_one(&self) -> Vec<Self> {
            let mut n = self.clone();
            let mut v = vec![n.clone()];
            while !n.is_one() {
                n = n.rules();
                v.push(n.clone());
            }
            v
        }
    }
}

#[allow(clippy::unwrap_used, unused_imports)]
mod tests {
    use beetle_nonzero::{
        ranges::{RangeNonZeroBigUint, RangeNonZeroUnsigned},
        traits::ToNonZero,
        NonZero, NonZeroBigUint,
    };

    use crate::Steps;

    // Number of steps to reach 1 for integers 1..=72 (according to the Online Encyclopedia of Integer Sequences)
    // For some reason this is counted as dead code currently, but it isn't dead code, I just convert it to a vec, or iter when using it.
    #[allow(dead_code)]
    const OEIS_STEPS: [u64; 72] = [
        0, 1, 7, 2, 5, 8, 16, 3, 19, 6, 14, 9, 9, 17, 17, 4, 12, 20, 20, 7, 7, 15, 15, 10, 23, 10,
        111, 18, 18, 18, 106, 5, 26, 13, 13, 21, 21, 21, 34, 8, 109, 8, 29, 16, 16, 16, 104, 11,
        24, 24, 24, 11, 11, 112, 112, 19, 32, 19, 32, 19, 19, 107, 107, 6, 27, 27, 27, 14, 14, 14,
        102, 22,
    ];

    #[test]
    fn step_counts_for_integers_are_correct() {
        use crate::traits::Steps;
        use beetle_nonzero::NonZero;
        use num::One;

        // u8
        let one: NonZero<u8> = NonZero::one();
        assert_eq!(one.steps_to_one(), 0);

        // u16
        let one: NonZero<u16> = NonZero::one();
        assert_eq!(one.steps_to_one(), 0);

        // u32
        let one: NonZero<u32> = NonZero::one();
        assert_eq!(one.steps_to_one(), 0);

        // u64
        let one: NonZero<u64> = NonZero::one();
        assert_eq!(one.steps_to_one(), 0);

        // u128
        let one: NonZero<u128> = NonZero::one();
        assert_eq!(one.steps_to_one(), 0);

        // BigUint
        let one: NonZeroBigUint = NonZeroBigUint::one();
        assert_eq!(one.steps_to_one(), 0);
    }

    #[test]
    fn step_counts_for_ranges_are_correct() {
        use beetle_nonzero::{ranges::RangeNonZeroUnsigned, NonZero};

        // U32
        let start: u32 = 1;
        let stop: u32 = 73;
        let steps: Vec<u64> = RangeNonZeroUnsigned::from_primitives(start, stop)
            .unwrap()
            .map(|n: NonZero<u32>| n.steps_to_one())
            .collect();
        assert_eq!(steps, OEIS_STEPS.to_vec());

        // BigUint
        let start: u32 = 1;
        let stop: u32 = 73;
        let steps: Vec<u64> = RangeNonZeroBigUint::from_biguints(start.into(), stop.into())
            .unwrap()
            .map(|n| n.steps_to_one())
            .collect();
        assert_eq!(steps, OEIS_STEPS.to_vec());
    }

    #[test]
    fn transformations_for_numbers_are_correct() {
        use crate::Transformations;
        let n: NonZero<u32> = 4u32.to_nonzero().unwrap();
        let transforms = n.transformations_to_one();
        let expected_transformations: Vec<NonZero<u32>> = [4, 2, 1]
            .into_iter()
            .map(|x: u32| x.to_nonzero().unwrap())
            .collect();
        assert_eq!(transforms, expected_transformations);
    }
}