From 7bf2f073d79fd1e7e45e14ce99cf5de06c06c90f Mon Sep 17 00:00:00 2001 From: Dan Ballard Date: Thu, 21 Aug 2014 23:11:17 -0700 Subject: [PATCH] e3 ghc haskel --- e3/e3.ghc.hs | 29 +++++++++++++++++++++++++++++ haskell.txt | 4 ++++ 2 files changed, 33 insertions(+) create mode 100644 e3/e3.ghc.hs 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