Optimizations
The As a parity sequence section above gives a way to speed up simulation of the sequence. To jump ahead k steps on each iteration (using the f function from that section), break up the current number into two parts, b (the k least significant bits, interpreted as an integer), and a (the rest of the bits as an integer). The result of jumping ahead k steps can be found as:
- f k(a 2k + b) = a 3c(b) + d(b).
The c and d arrays are precalculated for all possible k-bit numbers b, where d(b) is the result of applying the f function k times to b, and c(b) is the number of odd numbers encountered on the way. For example, if k=5, you can jump ahead 5 steps on each iteration by separating out the 5 least significant bits of a number and using:
- c(0..31) = {0,3,2,2,2,2,2,4,1,4,1,3,2,2,3,4,1,2,3,3,1,1,3,3,2,3,2,4,3,3,4,5}
- d(0..31) = {0,2,1,1,2,2,2,20,1,26,1,10,4,4,13,40,2,5,17,17,2,2,20,20,8,22,8,71,26,26,80,242}.
This requires 2k precomputation and storage to speed up the resulting calculation by a factor of k, a space-time tradeoff.
For the special purpose of searching for a counterexample to the Collatz conjecture, this precomputation leads to an even more important acceleration due to Tomás Oliveira e Silva and is used in the record confirmation of the Collatz conjecture. If, for some given b and k, the inequality
- f k(a 2k + b) = a 3c(b) + d(b) < a 2k + b
holds for all a, then the first counterexample, if it exists, cannot be b modulo 2k. For instance, the first counterexample must be odd because f(2n) = n; and it must be 3 mod 4 because f2(4n + 1) = 3n + 1. For each starting value a which is not a counterexample to the Collatz conjecture, there is a k for which such an inequality holds, so checking the Collatz conjecture for one starting value is as good as checking an entire congruence class. As k increases, the search only needs to check those residues b that are not eliminated by lower values of k. On the order of 3k/2 residues survive. For example, the only surviving residues mod 32 are 7, 15, 27, and 31; only 573,162 residues survive mod 225 = 33,554,432.
Read more about this topic: Collatz Conjecture