diff options
| author | Matthias Wisniowski <matthias.wisniowski@gmail.com> | 2012-03-26 15:17:27 +0200 |
|---|---|---|
| committer | Matthias Wisniowski <matthias.wisniowski@gmail.com> | 2012-03-26 15:17:27 +0200 |
| commit | 96e23eedb94c95b2758db0733f8bb5d823908b87 (patch) | |
| tree | 7bc7f97efe28bfd02477860db06c109521d66dd6 | |
| parent | 8613f7154f815835fc5e6ddbe15e9aebc62109b1 (diff) | |
| download | ffp-96e23eedb94c95b2758db0733f8bb5d823908b87.tar.gz ffp-96e23eedb94c95b2758db0733f8bb5d823908b87.tar.bz2 ffp-96e23eedb94c95b2758db0733f8bb5d823908b87.zip | |
Increased performance of 2.1
Implemented a faster prime-generator and a primality-check.
| -rw-r--r-- | AufgabeFFP2.hs | 31 |
1 files changed, 17 insertions, 14 deletions
diff --git a/AufgabeFFP2.hs b/AufgabeFFP2.hs index 7993c01..44b2efb 100644 --- a/AufgabeFFP2.hs +++ b/AufgabeFFP2.hs | |||
| @@ -3,23 +3,26 @@ where | |||
| 3 | 3 | ||
| 4 | -- 1 | 4 | -- 1 |
| 5 | 5 | ||
| 6 | -- primality check | ||
| 7 | isPrime :: Integer -> Bool | ||
| 8 | isPrime n = n > 1 && | ||
| 9 | foldr (\p r -> | ||
| 10 | p*p > n | ||
| 11 | || ( | ||
| 12 | (n `mod` p) /= 0 | ||
| 13 | && r | ||
| 14 | ) | ||
| 15 | ) | ||
| 16 | True primes | ||
| 17 | |||
| 18 | -- series of primes | ||
| 6 | primes :: [Integer] | 19 | primes :: [Integer] |
| 7 | primes = sieve [2 ..] | 20 | primes = 2:filter isPrime [3,5..] |
| 8 | |||
| 9 | -- sieve of eratosthenes (according to lecture slides) | ||
| 10 | sieve :: [Integer] -> [Integer] | ||
| 11 | sieve (x:xs) = x : sieve [ y | y <- xs, mod y x > 0] | ||
| 12 | 21 | ||
| 13 | -- generate all pairs with map and then filter only the valid ones | 22 | -- generate all pairs with map and then filter only the valid ones |
| 14 | -- TODO: filtering should be optimized, still a bit slow | 23 | -- pair is valid if the second component n is a prime |
| 15 | pps :: [(Integer, Integer)] | 24 | pps :: [(Integer, Integer)] |
| 16 | pps = filter validPair $ map (\x -> (x,x+2)) primes | 25 | pps = filter (\(_,y) -> isPrime y) $ map (\x -> (x,x+2)) primes |
| 17 | where | ||
| 18 | -- pair is valid if the second component n is a prime | ||
| 19 | -- since simply checking if the Integer is element of the prime-list | ||
| 20 | -- does not terminate for non-primes, we take the first n primes | ||
| 21 | validPair :: (Integer,Integer) -> Bool | ||
| 22 | validPair (_,maybePrime) = elem maybePrime $ take (fromInteger maybePrime) primes | ||
| 23 | 26 | ||
| 24 | ------------------------------------------------------------------------------- | 27 | ------------------------------------------------------------------------------- |
| 25 | 28 | ||
| @@ -27,7 +30,7 @@ pps = filter validPair $ map (\x -> (x,x+2)) primes | |||
| 27 | 30 | ||
| 28 | -- generates powers of 2 | 31 | -- generates powers of 2 |
| 29 | pof2s :: [Integer] | 32 | pof2s :: [Integer] |
| 30 | pof2s = [1] ++ (map (2*) pof2s) | 33 | pof2s = [1] ++ map (2*) pof2s |
| 31 | 34 | ||
| 32 | pow :: Int -> Integer | 35 | pow :: Int -> Integer |
| 33 | pow 0 = 1 | 36 | pow 0 = 1 |
