1
+ {-# LANGUAGE TupleSections #-}
2
+
1
3
{-# OPTIONS_GHC -fno-warn-type-defaults #-}
2
4
3
5
module Math.NumberTheory.SequenceBench
@@ -13,42 +15,33 @@ import Data.Maybe
13
15
import Math.NumberTheory.Primes
14
16
15
17
filterIsPrime :: (Integer , Integer ) -> Integer
16
- filterIsPrime (p, q) = sum $ takeWhile (<= q) $ dropWhile (< p) $ filter (isJust . isPrime) (map toPrim [toIdx p .. toIdx q ])
18
+ filterIsPrime (p, q) = sum $ takeWhile (<= p + q) $ dropWhile (< p) $ filter (isJust . isPrime) (map toPrim [toIdx p .. toIdx (p + q) ])
17
19
18
20
eratosthenes :: (Integer , Integer ) -> Integer
19
- eratosthenes (p, q) = sum (map unPrime [nextPrime p .. precPrime q ])
21
+ eratosthenes (p, q) = sum (map unPrime [nextPrime p .. precPrime (p + q) ])
20
22
21
23
atkin :: (Integer , Integer ) -> Integer
22
- atkin (p, q) = toInteger $ sum $ atkinPrimeList $ atkinSieve (fromInteger p) (fromInteger $ q - p )
24
+ atkin (p, q) = toInteger $ sum $ atkinPrimeList $ atkinSieve (fromInteger p) (fromInteger q )
23
25
24
26
filterIsPrimeBench :: Benchmark
25
27
filterIsPrimeBench = bgroup " filterIsPrime" $
26
- [ bench (show (10 ^ x, 10 ^ y)) $ nf filterIsPrime (10 ^ x, 10 ^ x + 10 ^ y )
28
+ [ bench (show (10 ^ x, 10 ^ y)) $ nf filterIsPrime (10 ^ x, 10 ^ x)
27
29
| x <- [5 .. 8 ]
28
30
, y <- [3 .. x- 1 ]
29
31
]
30
32
31
- eratosthenesBench :: Benchmark
32
- eratosthenesBench = bgroup " eratosthenes" $
33
- [ bench (show (10 ^ x, 10 ^ y)) $ nf eratosthenes (10 ^ x, 10 ^ x + 10 ^ y)
34
- | x <- [10 .. 17 ]
35
- , y <- [6 .. x- 1 ]
36
- , x == 10 || y == 7
37
- ]
38
-
39
- atkinBench :: Benchmark
40
- atkinBench = bgroup " atkin" $
41
- [ bench (show (10 ^ x, 10 ^ y)) $ nf atkin (10 ^ x, 10 ^ x + 10 ^ y)
42
- | x <- [10 .. 17 ]
43
- , y <- [6 .. x- 1 ]
44
- , x == 10 || y == 7
33
+ sieveBench :: Benchmark
34
+ sieveBench = bgroup " sieve" $ concat
35
+ [ [ bench (" eratosthenes/" ++ show (10 ^ x, 10 ^ y)) $ nf eratosthenes (10 ^ x, 10 ^ y)
36
+ , bench (" atkin/" ++ show (10 ^ x, 10 ^ y)) $ nf atkin (10 ^ x, 10 ^ y)
37
+ ]
38
+ | (x, y) <- map (10 ,) [6 .. 9 ] ++ map (,7 ) [10 .. 16 ]
45
39
]
46
40
47
41
benchSuite :: Benchmark
48
42
benchSuite = bgroup " Sequence"
49
- [ filterIsPrimeBench
50
- , eratosthenesBench
51
- , atkinBench
43
+ [ sieveBench
44
+ , filterIsPrimeBench
52
45
]
53
46
54
47
-------------------------------------------------------------------------------
0 commit comments