-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path74.hs
More file actions
83 lines (67 loc) · 2.21 KB
/
74.hs
File metadata and controls
83 lines (67 loc) · 2.21 KB
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
{-# OPTIONS_GHC -O3 #-}
{-# LANGUAGE BangPatterns #-}
import Data.List
import Control.Monad
import Control.Applicative
import Data.Array
import qualified Data.ByteString.Lazy.Char8 as B
primes :: [Int]
primes = 2: 3: sieve 0 (tail primes) 5
sieve k (p:ps) x = [n | n <- [x,x+2..p*p-2], and [n `rem`p /=0 | p <- fs]]
++ sieve (k+1) ps (p*p+2)
where fs = take k (tail primes)
memoized_divisors' :: Int -> [Int]
memoized_divisors' n | n > size = divisors' n
| otherwise = memoArray ! n
where size = 100000
memoArray = listArray (0,size) (map divisors' [0 .. size])
-- divisors' = map product
-- . init
-- . sequence
-- . map (scanl (*) 1)
-- . group
-- . primeFactors
divisors' :: Int -> [Int]
divisors' = map product
. init
. mapM (scanl (*) 1)
. group
. primeFactors
primeFactors :: Int -> [Int]
primeFactors 1 = []
primeFactors n = foo n primes
where
size = 100000
memoArray = listArray (0,size) (map (memoGo primes) [0 .. size])
foo n ps@(p:pt)
| n <= size = memoArray ! n
| p*p > n = [n]
| n `rem` p == 0 = p:foo (n `quot` p) ps
| otherwise = memoGo pt n
memoGo ps@(p:pt) n
| p*p > n = [n]
| n `rem` p == 0 = p:memoGo ps (n `quot` p)
| otherwise = memoGo pt n
-- primeFactors' 1 = []
-- primeFactors' n = go n primes
-- where
-- go n ps@(p:pt)
-- | p*p > n = [n]
-- | n `rem` p == 0 = p : go (n `quot` p) ps
-- | otherwise = go n pt
-- divisors :: Integer -> [Integer]
-- divisors 1 = []
-- divisors n = -- (1:)
-- nub
-- . map product
-- . tail
-- . powerset'
-- -- . filterM (const [False,True])
-- $ primeFactors n
-- powerset' [] = [[]]
-- powerset' (x:xs) = concatMap (\ys -> [x:ys,ys]) (powerset' xs)
main = do
_:ls <- B.lines <$> B.getContents
mapM_ (print . sum . memoized_divisors' . takeInt . B.readInt) ls
return ()
takeInt (Just (!x,y)) = x