Skip to content

A code challenge to create a sequence of numbers based on the Collatz Conjecture.

Notifications You must be signed in to change notification settings

eslutz/Collatz-Sequence

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

61 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Code Challenge: Collatz Sequence

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)

Collatz Function

How to Start the Challenge

  1. Fork this project to your account (Forking a repository)
  2. Clone the forked repo to your computer (Cloning a repository)
  3. Start a new project, in a language of your choosing, in the cloned repo
  4. Complete the requirements listed in Requirements
  5. 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

Requirements

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

Validation

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


References

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

About

A code challenge to create a sequence of numbers based on the Collatz Conjecture.

Topics

Resources

Code of conduct

Stars

Watchers

Forks

Languages

  • C++ 20.9%
  • Java 19.6%
  • C# 18.8%
  • TypeScript 17.3%
  • Python 15.9%
  • PHP 7.3%
  • JavaScript 0.2%