Java and Python program to perform Collatz Conjecture
Collatz Conjecture is a hypothesis in Mathematics that is basically a sequence which can be explained as- initiate with a number 'n', and check if it is an even number; if so, then the subsequent number should be the half of it, and if not, then the subsequent number should be '3*n + 1'. The hypothesis is irrespective of the value of the number 'n' to which the sequence is started; the sequence will always include 1.
Considering the above conjecture, let's proceed to see how the programming questions are generally asked.
Here, the question is taken from the 'Python Programming Essentials' course on Coursera, and the question is:
Consider the simple function f(n) that takes an integer n and divides it by two if n is even and multiplies n by 3; it adds one to the result if n is odd. The conjecture involves studying the value of expressions of the form f(f(f(...f(f(n))))) as the number of calls to the function f increases. The conjecture is that, for any non-negative integer n, repeated application of 'f' to 'n' yields a sequence of integers that always includes 1.
Your task for this question is to implement the Collatz function f in Python. The key to your implementation is to build a test that determines whether n is even or odd by checking whether the remainder when n is divided by 2 is either zero or one.
Once you have implemented f, test the your implementation on the expression f(f(f(f(f(f(f(674))))))). This expression should evaluate to 190. Finally, compute the value of the expression f(f(f(f(f(f(f(f(f(f(f(f(f(f(1071)))))))))))))) and enter the result below as an integer.
Now, see how this conjecture could be implemented as simple programs in Python & Java languages.