diff --git a/e3/e3.ghc.hs b/e3/e3.ghc.hs new file mode 100644 index 0000000..dc7a866 --- /dev/null +++ b/e3/e3.ghc.hs @@ -0,0 +1,29 @@ +{- _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)) diff --git a/haskell.txt b/haskell.txt index f7efb72..23df9a3 100644 --- a/haskell.txt +++ b/haskell.txt @@ -1,3 +1,7 @@ +ghc file + +OR + hugs :load file fn