e3 ghc haskel

This commit is contained in:
Dan Ballard 2014-08-21 23:11:17 -07:00
parent bb6dc490ca
commit 7bf2f073d7
2 changed files with 33 additions and 0 deletions

29
e3/e3.ghc.hs Normal file
View File

@ -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))

View File

@ -1,3 +1,7 @@
ghc file
OR
hugs
:load file
fn