Skip to content

Instantly share code, notes, and snippets.

@DavidBrower
Last active March 29, 2016 04:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DavidBrower/e0f303b5c8c0329c0c3e to your computer and use it in GitHub Desktop.
Save DavidBrower/e0f303b5c8c0329c0c3e to your computer and use it in GitHub Desktop.
Smallest Common Multiple
open System
let isDivisibleBy seq n =
seq
|> Seq.forall (fun x -> n % x = 0)
Seq.unfold (fun x -> Some(x, x + 20)) 20
|> Seq.find (isDivisibleBy [|1 .. 20|])
@DavidBrower
Copy link
Author

Finally, by lazily generating a sequence of numbers that are multiples of 20 we can greatly improve performance:

Seq.unfold (fun x -> Some(x, x + 20)) 20
    |> Seq.find (isDivisibleBy [|1 .. 20|])
Real: 00:00:08.322, CPU: 00:00:09.448, GC gen0: 279, gen1: 12
val it : int = 232792560

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment