Collatz Conjecture
InterviewTime limit1sMemory limit128 MB
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 using these rules.
- If is even, then .
- If is odd, then .
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 .
Now for the problem.
You are given two positive integers and . Build a Collatz sequence from each of them. Comparing the two sequences from the beginning, find the first value 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 and (). 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 is the first value that appears in both the sequence of and the sequence of , and and are the positions at which first appears in the sequence of and of , respectively. Positions are counted with the first term as 0.