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
functionSmallestMultiple(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 = nwhiletrue count =0for index =1:niflcm(multiple,index) ==lcm(multiple,1) count = count +1elsecontinueendendif count == n smallest_multiple = multiplebreakend multiple = multiple +1endreturn smallest_multipleend
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.