Collatz Sequence
excerpt: How to write recursive algorithm using the recurrence relation. tags: modulo-operator recurrence-relation reducing-the-input-value counter
A Collatz sequence in mathematics can be defined as follows. Starting with any positive integer:
if n is even, the next number in the sequence is n / 2 if n is odd, the next number in the sequence is 3n + 1
It is conjectured that every such sequence eventually reaches the number 1. Test this conjecture.
Implementation
Recursive Solution
|
|
Iterative Solution
|
|
Problem Variation
Count the number of steps required to reach 1 in the Collatz sequence.
Iterative Solution
|
|
Recursive Solution
|
|
Building Blocks
- Modulo Operator
- Recurrence Relation
- Reducing the Input Value
- Counter