PROBLEM LINK: Contest Page | CodeChef
Author: Setter’s name
Tester: Tester’s name
Editorialist: Editorialist’s name
DIFFICULTY : BEGINNER
You are asked to generate a sequence in such a way that you are given the first element of the sequence. Next element is determined by the value of the current element. If the current element is an even number then the next element will be half of the current element. Else if it is odd then the next element is one added to 3 times to the value of the current element.The sequence ends when it reaches 1.
Enter a positive value and perform collatz algorithm on it and display the sequence.
The Collatz sequence is a sequence of numbers relevant to the Collatz conjecture, which theorizes that any number using this algorithm will eventually be reduced to 1.The Collatz conjecture states that the sequence starts with any positive integer n. Then each term is obtained from the previous term as follows: if the previous term is even, the next term is one half of the previous term. If the previous term is odd, the next term is 3 times the previous term plus 1.
We take a positive number as input and enter a loop. Depending on whether it’s odd or even we’ll multiply it by 3 and add 1 to it or divide it by 2. We keep on doing this until we get 1 as the value.