e3 ghc haskel
This commit is contained in:
parent
bb6dc490ca
commit
7bf2f073d7
|
@ -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))
|
|
@ -1,3 +1,7 @@
|
||||||
|
ghc file
|
||||||
|
|
||||||
|
OR
|
||||||
|
|
||||||
hugs
|
hugs
|
||||||
:load file
|
:load file
|
||||||
fn
|
fn
|
||||||
|
|
Loading…
Reference in New Issue