Prof. Fumblemore and the Collatz Conjecture
InterviewTime limit1sMemory limit1024 MB
Given a string of E and O letters, find the smallest positive integer whose Collatz sequence type matches it, or report INVALID for a malformed string.
- Level
Medium4 of 10
- Topics
- Brute force, Simulation, Implementation
- Solved
- No attempts yet
Problem
The Collatz function on positive integers is:
- if is even
- if is odd
The Collatz sequence of a positive integer is
For example,
The Collatz Conjecture (also known as the 3n+1 problem) states that for every positive integer eventually ends by repeating 4, 2, 1. The status of this conjecture is still unknown. No proof has been given, and no counterexample has been found up to very large values.
Prof. Fumblemore wants to study the problem using Collatz sequence types. The Collatz sequence type (CST) of an integer , , is a sequence of the letters E and O (for even and odd) that describe the parity of the values in up to but not including the first power of 2. So,
Note that
so 12 and 908 have the same CST.
Prof. Fumblemore needs a program that takes a sequence of E's and O's and returns the smallest integer for which the given sequence is .
Notes:
- E's are even numbers that are not powers of 2.
- O's are odd numbers greater than 1.
- The last letter in a sequence must be an O (if is a power of 2, so is ).
- Two O's cannot be adjacent, since is even.
- Prof. Fumblemore does not type well, so you must check that the input sequence is valid before attempting to find . That is, the sequence contains only E's and O's, ends in O, and has no two O's adjacent.
Input
One line containing a string of up to 50 letters, each either E or O.
Output
One line containing INVALID if the input sequence is invalid. Otherwise, one line containing the smallest integer such that is the input sequence. The input is chosen so that .