Question: Collatz Conjecture (Java programming) Take any integer n and apply the following rule: If n=1: stop and dont do anything If n is even: divide
Collatz Conjecture (Java programming)
Take any integer n and apply the following rule:
If n=1: stop and dont do anything
If n is even: divide it by 2 (type A)
If n is odd: multiply it by 3 and add 1 (type B)
The conjecture states that given any number n, if we repeatedly apply the above process, we will always eventually reach the value 1, and stop.
For instance, given the value 3 we have 3 -> 10 -> 5-> 16 -> 8 -> 4 -> 2 -> 1
Reaching the value 1 after 7 operations of type A or of type B.
Write a Java method int Collatz(int n) that takes as parameter an integer number greater than 1 and it outputs the number of operations (type A or type B) necessary to reach the value 1. Test your method within a class and check that it works properly.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
