Project Euler’s Problem #5
Problem
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 number that is evenly divisible by all of the numbers from 1 to 20?
Solution #1
//----------------------------------------------------------------------------- // From: www.slashgeek.org // Proj: Problem5 // File: Program.fs // Desc: This is my take on Project Euler's problem #5 //----------------------------------------------------------------------------- // Problem: // 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 number that is evenly divisible by all of the numbers // from 1 to 20? #light // Starts the stopwatch. let stopwatch = System.Diagnostics.Stopwatch.StartNew() // Tests if an integer is divisible by [1;20]. let isDivisibleBy1To20 c = let mutable divisible = true let mutable i = 1 while i <= 20 do if c % i <> 0 then divisible <- false i <- 21 i <- i + 1 divisible = true // Tests all integers, starting from 20. let mutable n = 20 let mutable found = false while not found do if isDivisibleBy1To20 n then found <- true else n <- n + 20 // Stop the stopwatch. stopwatch.Stop() // Print the result. printfn "The result is %d" n printfn "Time elapsed is %d milliseconds" stopwatch.ElapsedMilliseconds
Solution #2 (by hand)
// Compute the prime factorization of each number from 1 to 20, and multiply the
// greatest power of each prime together:
//
// 20 = 2^2 * 5
// 19 = 19
// 18 = 2 * 3^2
// 17 = 17
// 16 = 2^4
// 15 = 3 * 5
// 14 = 2 * 7
// 13 = 13
// 11 = 11
//
// All others are included in the previous numbers.
//
// ANSWER: 2^4 * 3^2 * 5 * 7 * 11 * 13 * 17 * 19 = 232 792 560
