So what is the Collatz Sequence? The short answer is, it's a sequence of numbers generated following a mathematical formula that when starting with any positive integer always ends with 1.
The Collatz Conjecture is one of the most famous unsolved problems in mathematics. The conjecture asks whether repeating two simple arithmetic operations will eventually transform every positive integer into 1. It concerns sequences of integers in which each term is obtained from the previous term as follows: if the previous term is even, the next term is one half of the previous term. If the previous term is odd, the next term is 3 times the previous term plus 1. The conjecture is that these sequences always reach 1, no matter which positive integer is chosen to start the sequence. (Wikipedia contributors, 2022)
- Fork this project to your account (Forking a repository)
- Clone the forked repo to your computer (Cloning a repository)
- Start a new project, in a language of your choosing, in the cloned repo
- Complete the requirements listed in Requirements
- Validate your program against the values in Validation
Join in on the discussion in our Discord code-challenge channel!
- Share your solution
- Ask questions
- Check out the solutions others have come up with
- Provide feedback on the code challenge itself
Write a console program that meets the following requirements:
- Get user input for any positive integer
- Ensure that the user enters valid input
- Generate the Collatz Sequence
- If the number is even, divide the number by 2 to get the next number
- If the number is odd, multiply the number by 3 and add 1 to get the next number
- Continue until you reach the final number of the sequence, 1
- Display the sequence
- Prompt for a new starting number and generate sequence until the user decides to quit
Enter the below values to get the Collatz Sequence. Compare the length of your generated sequence to the length listed with the value below. If they are the same then your sequence is correct. Sequence length values verified using Collatz Conjecture 3n+1 Calculator - Online Sequence Algorithm.
Input Value | Sequence Length |
---|---|
5 | 6 |
50 | 25 |
5000 | 29 |
5000000 | 145 |
5000000000 | 124 |
5000000000000000000000000000000000000 | 653 |
Collatz Conjecture 3n+1 Calculator - Online Sequence Algorithm. (n.d.). dCode. Retrieved August 6, 2022, from https://www.dcode.fr/collatz-conjecture
Wikipedia contributors. (2022, June 30). Collatz conjecture. Wikipedia. https://en.wikipedia.org/wiki/Collatz_conjecture