In a Collatz sequence, all of its terms are positive integers which satisfy the following recursive formula an=0.5an−1 if an−1 is even an=3an−1+1 if an−1 is odd Directions: Given an initial term implement a Collatz sequence, and find its length. Input: a1 Output: the length of the Collatz sequence Don't use some sort of menu loop to get user input
In a Collatz sequence, all of its terms are positive integers which satisfy the following recursive formula
an=0.5an−1 if an−1 is even
an=3an−1+1 if an−1 is odd
Directions: Given an initial term implement a Collatz sequence, and find its length.
Input: a1
Output: the length of the Collatz sequence
Don't use some sort of menu loop to get user input

The Algorithm of the code:-
1. Begin
2. Initialize sequence_length = 1,
3. Input the initial term a1.
4. Print “Terms of the sequence are:”
5. While n is not equal to 1, do the following
a. Increment sequence_length by 1
b. Print n
c. If n is odd,
n = 3*n + 1
d. Else if n is even,
n = 0.5*n
6. Print n and sequence_length
7. End
Step by step
Solved in 4 steps with 2 images









