Created
September 21, 2015 21:23
-
-
Save kirca/209fcc608cf41f65b65d to your computer and use it in GitHub Desktop.
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
isPrime :: Int -> Bool | |
isPrime n = null $ dropWhile (not . (n `isDivisableWith`)) [2..n-1] | |
isDivisableWith :: Int -> Int -> Bool | |
isDivisableWith a b = a `mod` b == 0 | |
primes = filter isPrime [1..] | |
largestPrimeFactor :: Int -> Int | |
largestPrimeFactor 1 = 1 | |
largestPrimeFactor n = helper n (tail primes) | |
where helper 1 (prime:_) = prime | |
helper n (prime:primes) | |
| n `isDivisableWith` prime = helper (n `div` prime) (prime:primes) | |
| otherwise = helper n primes |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment