13
3
I think the Collatz Conjecture is already well-known. But what if we invert the rules?
Start with an integer n >= 1.
Repeat the following steps:
If n is even, multiply it by 3 and add 1.
If n is odd, subtract 1 and divide it by 2.
Stop when it reaches 0
Print the iterated numbers.
Test cases:
1 => 1, 0
2 => 2, 7, 3, 1, 0
3 => 3, 1, 0
10 => 10, 31, 15, 7, 3...
14 => 14, 43, 21, 10, ...
Rules:
This sequence does not work for a lot of numbers because it enters in an infinite loop. You do not need to handle those cases. Only printing the test cases above is enough.
I suggested to subtract 1 and divide by two to give a valid integer to continue, but it is not required to be computed that way. You may divide by 2 and cast to integer or whatever other methods that will give the expected output.
You need to print the initial input as well.
The output does not need to be formatted as the test cases. It was just a suggestion. However, the iterated order must be respected.
The smallest code wins.
9
As this is your third question in as many hours, I'd recommend that you check out the Sandbox, the place where we usually post question drafts for feedback, and to make sure they aren't duplicates.
– caird coinheringaahing – 2018-11-04T20:38:15.710Thank you @cairdcoinheringaahing. I didn't know about this page. – Eduardo Hoefel – 2018-11-04T21:38:57.870
Do we have to print the
0
at the end? – flawr – 2018-11-04T23:01:19.6372You might want to expand the last two test cases, since they're not that long – Jo King – 2018-11-04T23:07:41.840
3@JoKing I compressed it because it repeats the output from the other lines. At the point you reach 3, it has the same output of when you start from it. The same applies for 10 or any other number. – Eduardo Hoefel – 2018-11-05T02:40:54.043
@flawr it's optional. – Eduardo Hoefel – 2018-11-05T02:41:27.663
Does it have to be in the same format as in the test cases? – MilkyWay90 – 2018-12-02T05:24:09.910
@MilkyWay90: not same format, but they must be in decimal format, keep the ordering and have some kind of separator character that distinguishes the values. – Eduardo Hoefel – 2018-12-02T20:03:24.167