Skip to content

Instantly share code, notes, and snippets.

@DavidBrower
Last active March 12, 2016 21:54
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/61597608f4a50ef8fb2e to your computer and use it in GitHub Desktop.
Save DavidBrower/61597608f4a50ef8fb2e to your computer and use it in GitHub Desktop.
Largest Prime Factor
open System
let isPrime (n : int64) =
let upperBound = n |> float |> sqrt |> int64
seq {2L .. upperBound}
|> Seq.forall (fun x -> n % x <> 0L)
let getAllFactors (n : int64) =
let upperBound = n / 2L
seq {2L .. upperBound}
|> Seq.filter (fun x -> n % x = 0L)
let largestPrimeFactor (n : int64) =
getAllFactors n
|> Seq.filter isPrime
|> Seq.max
@DavidBrower
Copy link
Author

I've seen solutions where the upper bound when finding the factors of a number is the square root of that number. But isn't 5 a factor of 10?

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