-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEuler47.hs
37 lines (30 loc) · 951 Bytes
/
Euler47.hs
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
import Data.List
-- Sieve of Eratosthenes
mult n k = (n `mod` k) == 0
sqrt' :: Integral a => a -> Int
sqrt' = floor . sqrt . fromIntegral
isPrime 1 = False
isPrime 2 = True
isPrime n = all not $ map (mult n) $ takeWhile (<= sqrt' n) $ primes
primes = filter isPrime [2..]
{-
factors = factors' [] primes
factors' acc _ 1 = acc
factors' acc (p:ps) n
| n `mod` p == 0 = factors' (p:acc) (p:ps) (n `div` p)
| otherwise = factors' acc ps n
-}
factors = map (factors' primes) [0..]
factors' _ 0 = []
factors' _ 1 = []
factors' (p:ps) n
| n `mod` p == 0 = p : factors !! (n `div` p)
| otherwise = factors' ps n
factorCounts = map (length . nub) factors
k = 4
numsWithKFactors = map fst . filter (\(_,f) -> f == k) . zip [0..] $ factorCounts
firstInteresting all@(n:ns)
| take k all == [n..n-1+(fromIntegral k)] = n
| otherwise = firstInteresting ns
main = print . firstInteresting $ numsWithKFactors
--}