challenges/e3/e3.ghc.hs

30 lines
836 B
Haskell

{- _prime :: Int -> Int -> Int -> Bool
_prime n i max | i > max = True
| rem n i == 0 = False
| otherwise = _prime n (i + 1) max
prime :: Int -> Bool
prime n = _prime n 2 (ceiling (sqrt (fromIntegral n)))
-- Cannot handle prime nubmers
_first_factor :: Int -> Int -> Int -> Int
_first_factor n i max | rem n i == 0 = i
| otherwise = _first_factor n (i+1) max
first_factor n = _first_factor n 2 (ceiling (sqrt (fromIntegral n)))
factor :: Int -> [Int]
factor n | prime n = [n]
| otherwise = (factor x) ++ (factor y) where
x = first_factor n
y = div n x
-}
_factor n divisor | divisor > (ceiling (sqrt (fromIntegral n))) = [n]
| rem n divisor == 0 = (_factor (div n divisor) 2) ++ (_factor divisor 2)
| otherwise = _factor n (divisor + 1)
factor n = _factor n 2
main = print (maximum (factor 600851475143))