Project Euler

Author

Angel Alcala Ruiz

Published

November 20, 2023

Problem 5: Smallest Multiple

2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder.

What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20?

Let’s solve this problem using Julia

Code
function SmallestMultiple(n)

#' @@name SmallestMultiple
#'
#' @@description
#'
#' This function finds the smallest positive number that is evenly divisible by by all the numbers from 1 to n
#'
#' @@arg n: A positive integer n
#'
#' @@return The smallest positive number that is evenly divisible by by all the numbers from 1 to n
#'
#' @@examples
#'
#' n = 10
#' SmallestMultiple(n) 

    smallest_multiple = 0
    multiple = n

    while true
        count = 0
        for index = 1:n

            if lcm(multiple,index) == lcm(multiple,1)
                count = count + 1
            else
                continue
            end
        end

        if count == n
            smallest_multiple = multiple
            break
        end
        multiple = multiple + 1
    end

    return smallest_multiple
end
SmallestMultiple (generic function with 1 method)

Now let’s test our code with the given example

Code
SmallestMultiple(10)
2520

We can see that we get 2520 which is the smallest number that is evenly divisible by all of the numbers from 1 to 10.

Now let’s find the smallest multiple of all the numbers from 1 to 20

Code
SmallestMultiple(20)
232792560

Therefore we get that the smallest number that is evenly divisible by all of the numbers from 1 to 20 is 232,792,560.