In C++, write a method that follows the Collatz conjecture. If n is even set n=n/2 and if n is odd set n=3n+1.

Question

The Collatz conjecture states that when taking any natural number n, if n is even, set n = n /2. If n is odd, set n = 3n + 1. Repeat this process until n is equal to 1. The conjecture states that no matter what number you start with, you will always reach 1.

Explanation

Create a while loop where it will exit out of the loop once n is equal to 1. It will keep iterating in…

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store