Skip to content

Commit 65c36e5

Browse files
committed
BLD euler 5
1 parent dc0cfc6 commit 65c36e5

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

euler005.hs

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import Data.List
2+
3+
main = print $ product $ foldl1 lcm_primes $ map prime_factors [1..20]
4+
5+
lcm_primes :: (Eq a) => [a] -> [a] -> [a]
6+
lcm_primes a b = a ++ (b\\a)
7+
8+
prime_factors :: (Integral a) => a -> [a]
9+
prime_factors 1 = []
10+
prime_factors n
11+
| factor == [] = [n]
12+
| n < (head factor)^2 = [n] -- stop searching if sqrt n < head factor
13+
| otherwise = factor ++ prime_factors (n `div` (head factor))
14+
where factor = take 1 [x | x <- [2..n-1], n `rem` x == 0]
15+
16+
{-
17+
Naive solution: (takes too long and does not scale)
18+
main = print $ head $ filter evenlyDivisible1_20 [1..]
19+
20+
evenlyDivisible1_20 :: (Integral a) => a -> Bool
21+
evenlyDivisible1_20 n = and $ map (== 0) $ map (n `rem`) [1..20]
22+
-}

0 commit comments

Comments
 (0)