Skip to content

Instantly share code, notes, and snippets.

@kirca
Created September 21, 2015 21:23
Show Gist options
  • Save kirca/209fcc608cf41f65b65d to your computer and use it in GitHub Desktop.
Save kirca/209fcc608cf41f65b65d to your computer and use it in GitHub Desktop.
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