File tree Expand file tree Collapse file tree 1 file changed +22
-0
lines changed Expand file tree Collapse file tree 1 file changed +22
-0
lines changed Original file line number Diff line number Diff line change
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
+ -}
You can’t perform that action at this time.
0 commit comments