This page is still under construction.

Parts of this page are still being built. What you see may change.

Collatz Conjecture

Interview

Time limit1sMemory limit128 MB

Summary
For each pair A and B, generate both Collatz sequences up to 1 and find the first value they share, reporting its index in each sequence.
Level

Medium5 of 10

Topics
Simulation, Hash map, Math, Implementation
Solved
No attempts yet

Problem

The Collatz conjecture is a fascinating phenomenon. The rule looks simple, yet it has still not been proven mathematically. In this problem we assume that the conjecture is always true.

The Collatz conjecture is defined as follows. Build a sequence of positive integers xix_i using these rules.

  • If xix_i is even, then xi+1=xi/2x_{i+1} = x_i / 2.
  • If xix_i is odd, then xi+1=3×xi+1x_{i+1} = 3 \times x_i + 1.

The conjecture states that such a sequence eventually reaches 1. Using computers, scientists have verified that the conjecture holds whenever the first term is smaller than 2582^{58}.

Now for the problem.

You are given two positive integers AA and BB. Build a Collatz sequence from each of them. Comparing the two sequences from the beginning, find the first value CC that appears in both sequences, and determine at which position it appears in each sequence. Positions are counted with the first term as 0.

For convenience, a sequence stops as soon as it reaches 1 (after 1 the values would simply repeat as 1, 4, 2, 1, 4, 2, … forever).

Input

The input consists of several test cases. Each test case contains two integers AA and BB (1≤A,B≤1,000,0001 \le A, B \le 1{,}000{,}000). The last line contains two zeros and is not processed.

Output

For each test case, print one line in the following format.

A needs SA steps, B needs SB steps, they meet at C

Here CC is the first value that appears in both the sequence of AA and the sequence of BB, and SAS_A and SBS_B are the positions at which CC first appears in the sequence of AA and of BB, respectively. Positions are counted with the first term as 0.

Examples1

  1. Example 1

    Input
    7 8
    27 30
    0 0
    
    Expected output
    7 needs 13 steps, 8 needs 0 steps, they meet at 8
    27 needs 95 steps, 30 needs 2 steps, they meet at 46