Created
April 2, 2017 08:16
-
-
Save manofstick/84959c1b98541390604cf93a33ebcba3 to your computer and use it in GitHub Desktop.
TheBurningMonk - Euler - 5
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
open System.Diagnostics | |
module ``TheBurningMonk - Euler`` = | |
// collated from :- https://github.com/theburningmonk/ProjectEuler-FSharp-Solutions/tree/master/ProjectEulerSolutions/ProjectEulerSolutions | |
// documentation :- http://theburningmonk.com/project-euler-solutions/ | |
open System | |
open System.Numerics | |
let inline private validate expected received = | |
if expected <> received then | |
failwith <| sprintf "Invalid answer - expected=%A received=%A" expected received | |
let ``5`` () = | |
let isEvenlyDivided(n, m) = n % m = 0 | |
let isEvenlyDividedByAll(n, numbers) = numbers |> Seq.forall (fun x -> isEvenlyDivided(n, x)) | |
let findSmallestCommonMultiple(numbers) = | |
let max = Array.max(numbers) | |
Seq.unfold (fun x -> Some(x, x + 1)) 1 | |
|> Seq.map (fun x -> x * max) | |
|> Seq.filter (fun x -> isEvenlyDividedByAll(x, numbers)) | |
|> Seq.head | |
let commonMultiplier = findSmallestCommonMultiple [|1..20|] | |
validate 232792560 commonMultiplier | |
let ``5a`` () = | |
let isEvenlyDivided(n, m) = n % m = 0 | |
let isEvenlyDividedByAll(n, numbers) = numbers |> Seq.forall (fun x -> isEvenlyDivided(n, x)) | |
let findSmallestCommonMultiple(numbers) = | |
let max = Seq.max(numbers) | |
Seq.unfold (fun x -> Some(x, x + 1)) 1 | |
|> Seq.map (fun x -> x * max) | |
|> Seq.filter (fun x -> isEvenlyDividedByAll(x, numbers)) | |
|> Seq.head | |
let commonMultiplier = findSmallestCommonMultiple (Seq.ofArray [|1..20|]) | |
validate 232792560 commonMultiplier | |
let ``5b`` () = | |
let isEvenlyDivided(n, m) = n % m = 0 | |
let isEvenlyDividedByAll(n, numbers) = numbers |> Array.forall (fun x -> isEvenlyDivided(n, x)) | |
let findSmallestCommonMultiple(numbers) = | |
let max = Array.max(numbers) | |
Seq.unfold (fun x -> Some(x, x + 1)) 1 | |
|> Seq.map (fun x -> x * max) | |
|> Seq.filter (fun x -> isEvenlyDividedByAll(x, numbers)) | |
|> Seq.head | |
let commonMultiplier = findSmallestCommonMultiple [|1..20|] | |
validate 232792560 commonMultiplier | |
let time what f = | |
let sw = Stopwatch.StartNew () | |
let count = f 500000 | |
printfn "%s: %d (%d)" what sw.ElapsedMilliseconds count | |
let tests = [| | |
"TheBurningMonk - Euler - 5 |", ``TheBurningMonk - Euler``.``5``, 2 | |
"TheBurningMonk - Euler - 5a |", ``TheBurningMonk - Euler``.``5a``, 2 | |
"TheBurningMonk - Euler - 5b |", ``TheBurningMonk - Euler``.``5b``, 2 | |
|] | |
[<EntryPoint>] | |
let main argv = | |
printfn (match sizeof<nativeint> with | |
| 4 -> "32-bit" | |
| 8 -> "64-bit" | |
| _ -> "unknown bitage") | |
printf "warming up" | |
for _, f, iterations in tests do | |
if iterations > 1 then | |
f () | |
printf "." | |
printfn "\n" | |
for name, f, iterations in tests do | |
let sw = Stopwatch.StartNew () | |
for i = 1 to iterations do | |
f () | |
printfn "%s %d" name sw.ElapsedMilliseconds | |
0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment