Problem 47
Instead of building yet another function to calculate prime factors and sieves, I used an existing package: NumberTheory.Sieve.Factor to solve the 47th Euler problem:
import NumberTheory.Sieve.Factor
f l x = let l1 = zip l (map (length . factor (sieve x)) l)
l2 = filter ((==) 4 . snd) l1
l3 = consec l2
in l3
where consec (x1:x2:x3:x4:xs) =
case (fst x4) _-_ (fst x1) == 3 of
True -> [x1,x2,x3,x4]
False -> consec (x2:x3:x4:xs)
consec _ = []
main = print $ f [1..999999] 9999999
The function f calculates a list of pairs containing a number and the number of its prime factors, and filters all numbers that do not have 4 prime factors. Then it is a matter of calling the consec function, that returns the first 4 consecutive integers that have exactly 4 prime factors. The program executes in (0.38 secs, 143730392 bytes).
blog comments powered by Disqus