top of page

Problem 05

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

My Algorithm

For this problem, we need to find the smallest positive number that is evenly divisible by every number from one to twenty, inclusive. To do this, we can use the formula for the least common multiple of one to twenty. This formula can be expressed as: lcm(x, y) = (x * y) / gcd(x, y), where 'lcm' is the least common multiple and 'gcd' is the greatest common divisor. We can create a function for the 'gcd' by using Euclid's Algorithm and create another function for the 'lcm' using the formula described above. Using these two functions, we can loop from one to twenty taking the 'lcm' of each our answer and the current factor since the 'lcm' of 'n' numbers can be expressed as: lcm(x1, x2, ..., xn-1, xn) = lcm(lcm(x1, x2, ..., xn-1), xn). This results in our answer of 232792560.

Full Solution on GitHub

© 2023 by Matthew Woodring

    bottom of page