-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProblem3.hs
More file actions
34 lines (27 loc) · 945 Bytes
/
Problem3.hs
File metadata and controls
34 lines (27 loc) · 945 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
-- The prime factors of 13195 are 5, 7, 13 and 29.
-- What is the largest prime factor of the number 600851475143 ?
module Main where
largestPrimeFactor :: (Integral a) => a -> Maybe a
largestPrimeFactor a
| a <= 1 = Nothing
| a `mod` b == 0 && isPrime b = Just b
| otherwise = largestPrimeFactor' a (b - 1)
where b = round . sqrt $ fromIntegral a
largestPrimeFactor' :: (Integral a) => a -> a -> Maybe a
largestPrimeFactor' a b
| a <= 1 = Nothing
| b <= 1 = Nothing
| a `mod` b == 0 && isPrime b = Just b
| otherwise = largestPrimeFactor' a (b - 1)
isPrime :: (Integral a) => a -> Bool
isPrime a
| a <= 1 = False
| otherwise = isPrime' a (round . sqrt $ fromIntegral a)
isPrime' :: (Integral a) => a -> a -> Bool
isPrime' a b
| a <= 1 = False
| b <= 1 = True
| a `mod` b == 0 = False
| otherwise = isPrime' a (b - 1)
main :: IO ()
main = putStrLn . show $ largestPrimeFactor 600851475143