Cached Collatz P33673


Statement
 

pdf   zip

For any positive integer number nn, let C(n)C(n) be n/2n/2 if nn is even, and 3n+13n + 1 if nn is odd. The Collatz Conjecture states that the sequence n,C(n),C(C(n)),n, C(n), C(C(n)), \dots leads to 1 for any nn.

For example, let n=17n = 17. We get 17522613402010516842117 \rightarrow 52 \rightarrow 26 \rightarrow 13 \rightarrow 40 \rightarrow 20 \rightarrow 10 \rightarrow 5 \rightarrow 16 \rightarrow 8 \rightarrow 4 \rightarrow 2 \rightarrow 1, for a total of 12 steps. After that, the cycle 4, 2, 1 would keep repeating.

You want to make some computer analysis about the number of steps to get to 1 on several starting numbers, and leave the computer running overnight. To speed things up and avoid recalculation, you plan on storing all already computed numbers in a set. However, you realize that this would quickly exceed the memory constraints of your computer.

To avoid that, you discover LRU (Least Recently Used) caches, which can be helpful. Here’s how they work:

  • LRU caches have a fixed capacity KK. They map at most KK keys to their values.

  • Whenever a key is checked in the cache, its value will be returned if the key is present. Otherwise, the value will need to be computed, and then stored in the cache.

  • To store a new (key, value) when the cache is already full, the least recently used pair is evicted. We consider that we “use” a pair whenever we either store or check it.

With the help of this cache, your program will compute S(n)S(n) for some initial values nn in the following way:

  • S(n)S(n) will be computed recursively via the C(n)C(n) transformation. However, if nn is a cache hit (the key nn exists in the cache), S(n)S(n) is returned immediately. Otherwise, it’s a cache miss, and (n,S(n))(n, S(n)) is stored in the cache once S(n)S(n) is computed.

  • We will never store 1 as a key in the cache, and we can assume S(1)=0S(1) = 0.

For instance, this is the state of the cache for the first case of the sample input. It shows the (key, value) pairs, ordered from least to most recently used:

- After the 1st step: [] (The cache is empty.)
- After the 2nd step: [(2, 1)]
- After the 3nd step: [(2, 1)]
- After the 4th step: [(2, 1), (4, 2), (8, 3)] (Note that we can conclude S(8)=3S(8) = 3 only after we know S(4)=2S(4) = 2.)
- After the 5th step: [(16, 4), (32, 5), (64, 6)]
- After the 6th step: [(2, 1), (4, 2), (8, 3)]
- After the 7th step: [(4, 2), (8, 3), (2, 1)]
- After the 8th step: [(2, 1), (8, 3), (16, 4)]

Given KK and some starting values nn, please print S(n)S(n) and the number of cache misses to compute S(n)S(n) with the previous procedure.

Input

Input consists of several cases. Each case starts with KK, followed by a non-empty sequence n1,n2,n_1, n_2, \dots of positive integers ending with a 0. Assume 1K1041 \le K \le 10^4, and that the Collatz sequence of each given nin_i will never overflow with the usual 32-bit signed integers.

Output

For each nin_i, print S(ni)S(n_i), and the number of cache misses found while computing it. Suppose that the cache is empty at the beginning of each case. Print a line with 10 dashes at the end of every case.

Public test cases
  • Input

    3  1 2 2 8 64 8 2 16 0
    50  15 7 85 18 92 0
    

    Output

    1: 0 0
    2: 1 1
    2: 1 0
    8: 3 2
    64: 6 3
    8: 3 3
    2: 1 0
    16: 4 1
    ----------
    15: 17 17
    7: 16 8
    85: 9 5
    18: 20 4
    92: 17 1
    ----------
    
  • Information
    Author
    Víctor Chabrera
    Language
    English
    Official solutions
    C++
    User solutions
    C++