-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path005-smallest-multiple.lhs
executable file
·44 lines (31 loc) · 1.09 KB
/
005-smallest-multiple.lhs
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
35
36
37
38
39
40
41
42
43
44
#!/usr/bin/env runhaskell
[Smallest multiple](http://projecteuler.net/problem=5)
------------------------------------------------------
2520 is the smallest number that can be divided by each of the numbers from
1 to 10 without any remainder.
What is the smallest positive number that is evenly divisible by all of the
numbers from 1 to 20?
Code
----
> import Data.Numbers.Primes
> import Data.List
> import Control.Arrow
> highestCommonExponents =
> let f ps [] = ps
> f [] qs = qs
> f ((p,a):ps) ((q,b):qs) | p > q = (q,b) : f ((p,a):ps) qs
> | p < q = (p,a) : f ps ((q,b):qs)
> | otherwise = (p, max a b) : f ps qs
> in foldr1 f .
> map (map (head &&& length) . group . primeFactors)
> computeProduct :: [(Integer, Int)] -> Integer
> computeProduct = let g acc (p,a) = acc * p ^ a
> in foldl g 1
> main :: IO ()
> main = let result = computeProduct
$ highestCommonExponents [1 .. 20]
> :: Integer
> in print result
Answer
------
232792560