Skip to content

Latest commit

ย 

History

History
33 lines (20 loc) ยท 1.1 KB

File metadata and controls

33 lines (20 loc) ยท 1.1 KB

Description

The following iterative sequence is defined for the set of positive integers:

n โ†’ n/2 (n is even)
n โ†’ 3n + 1 (n is odd)

Using the rule above and starting with 13, we generate the following sequence:

13 โ†’ 40 โ†’ 20 โ†’ 10 โ†’ 5 โ†’ 16 โ†’ 8 โ†’ 4 โ†’ 2 โ†’ 1

It can be seen that this sequence (starting at 13 and finishing at 1) contains 10 terms. Although it has not been proved yet (Collatz Problem), it is thought that all starting numbers finish at 1.

Exercise

Which starting number, under one million, produces the longest chain?

NOTE: Once the chain starts the terms are allowed to go above one million.

Solution

You can find the algorithm to solve this challenge inside challenge.cpp, and the implementation of the Collatz sequence can be found inside tools/math/squences/collatz.cpp

Click here to see the result!

Result is: 837,799