Exercise 3: Collatz Conjecture Using Recursion
Calculate the sum of the digits of a number by using recursion.
We'll cover the following
Problem statement
Implement the recursive function to calculate the sequence of the Collatz Conjecture for a given number.
The Collatz Conjecture states that for any positive integer:
If the number is even, divide it by 2.
If the number is odd, multiply it by 3 and add 1.
Repeat the process for the resulting number until you reach 1.
Write a recursive function collatzSteps
that calculates the sequence of numbers in the Collatz Conjecture and returns the steps required to reach 1.
Sample input
Create a free account to access the full course.
Continue your learning journey with a 14-day free trial.
By signing up, you agree to Educative's Terms of Service and Privacy Policy