top of page

Problem 14

This is a C++ solution to Project Euler problem 14 found at: https://projecteuler.net/problem=14

My Algorithm

For this problem, we are asked to find the starting number, under one million, that produces the longest chain in the Collatz Sequence. Generating the Collatz Sequence is simple by using the rules of (n = n / 2) when 'n' is even, and (n = 3n + 1) when 'n' is odd. To solve this problem, we will first create a function that uses the above rules to generate the Collatz Sequence and return its size. Then, we just need to loop through all the numbers up to 1000000 and check whether the size of the Collatz Sequence for the current number we are checking is greater than the size of the current greatest size. If the number we are checking does have a larger size, we set it equal to the current greatest size. Finally, once the loop breaks, we can return the current greatest size which is 837799.

Full Solution on GitHub

© 2023 by Matthew Woodring

    bottom of page