Last active
March 12, 2016 21:54
-
-
Save DavidBrower/61597608f4a50ef8fb2e to your computer and use it in GitHub Desktop.
Largest Prime Factor
This file contains 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 | |
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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?