
IsaacRule
There’s a popular sequence in mathematics that starts with a given number, and proceeds according to the following rules:
- If the number is odd, multiply by 3 and add 1.
- If the number is even, divide by 2.
A well-known conjecture suggests that when you keep doing this, you will always reach 1 eventually.
For example, if you start with 4
, you reach 1
in 2
steps:4 -> 2 -> 1
If you started with 3
, you reach 1
in 7
steps:3 -> 10 -> 5 -> 16 -> 8 -> 4 -> 2 -> 1
David, a talented CodeSignal user, has studied this sequence for a long time, and he’s discovered an algorithm to find the total number of positive integers which require exactly K
steps to reach 1
for the first time. Because of his curiosity, he’d like to extend his algorithm so that it can work for any positive integer (not just 1
), but he’s having some trouble.
Given an integer steps
and an integer number
, let’s help David find the total number of positive integers which require exactly steps
to reach number
for the first time.
Examples
-
For
steps = 1
andnumber = 1
, the output should beIsaacRule(steps, number) = 1
.Only the number
2
will reach1
after exactly1
step:2 -> 1
So the answer is1
. -
For
steps = 2
andnumber = 5
, the output should beIsaacRule(steps, number) = 2
.3
and20
are the only two numbers that will reach5
after2
steps:3 -> 10 -> 5
and20 -> 10 -> 5
So the answer is2
.
Input / Output
-
[execution time limit] 0.5 seconds (cpp)
-
[input] integer steps
An integer representing the number of steps required to reach given
number
the first time.Guaranteed constraints:
0 ≤ steps ≤ 53
-
[input] integer number
An integer representing the number that will be reached after
steps
.Guaranteed constraints:
1 ≤ number ≤ 253 - steps
-
[output] integer
The total number of positive integers which require exactly
steps
to reachnumber
for the first time.
Post Comment