diff options
| -rw-r--r-- | AufgabeFFP2.hs | 36 | ||||
| -rw-r--r-- | TestAufgabeFFP2.hs | 70 |
2 files changed, 53 insertions, 53 deletions
diff --git a/AufgabeFFP2.hs b/AufgabeFFP2.hs index 7ed19fc..aef3bfb 100644 --- a/AufgabeFFP2.hs +++ b/AufgabeFFP2.hs | |||
| @@ -6,33 +6,33 @@ where | |||
| 6 | -- primality check | 6 | -- primality check |
| 7 | isPrime :: Integer -> Bool | 7 | isPrime :: Integer -> Bool |
| 8 | isPrime n = n > 1 && | 8 | isPrime n = n > 1 && |
| 9 | foldr (\p r -> | 9 | foldr (\p r -> |
| 10 | p*p > n | 10 | p*p > n |
| 11 | || ( | 11 | || ( |
| 12 | (n `mod` p) /= 0 | 12 | (n `mod` p) /= 0 |
| 13 | && r | 13 | && r |
| 14 | ) | 14 | ) |
| 15 | ) | 15 | ) |
| 16 | True primes | 16 | True primes |
| 17 | 17 | ||
| 18 | -- series of primes | 18 | -- series of primes |
| 19 | primes :: [Integer] | 19 | primes :: [Integer] |
| 20 | primes = 2:filter isPrime [3,5..] | 20 | primes = 2:filter isPrime [3,5..] |
| 21 | 21 | ||
| 22 | -- pairs of (p,p+2) | p,p+2 <- primes | 22 | -- pairs of (p,p+2) | p,p+2 <- primes |
| 23 | -- generate all pairs with map and then filter only the valid ones | 23 | -- generate all pairs with map and then filter only the valid ones |
| 24 | -- pair is valid if the second component n is a prime | 24 | -- pair is valid if the second component n is a prime |
| 25 | pps :: [(Integer, Integer)] | 25 | pps :: [(Integer, Integer)] |
| 26 | pps = filter (\(_,x) -> isPrime x) $ map (\p -> (p,p+2)) primes | 26 | pps = filter (\(_,x) -> isPrime x) $ map (\p -> (p,p+2)) primes |
| 27 | 27 | ||
| 28 | ------------------------------------------------------------------------------- | 28 | ------------------------------------------------------------------------------- |
| 29 | 29 | ||
| 30 | -- 2 | 30 | -- 2 |
| 31 | 31 | ||
| 32 | -- generates powers of 2 | 32 | -- generates powers of 2 |
| 33 | pof2s :: [Integer] | 33 | pof2s :: [Integer] |
| 34 | pof2s = [1] ++ map (2*) pof2s | 34 | pof2s = [1] ++ map (2*) pof2s |
| 35 | 35 | ||
| 36 | -- calculates 2^n | 36 | -- calculates 2^n |
| 37 | pow :: Int -> Integer | 37 | pow :: Int -> Integer |
| 38 | pow 0 = 1 | 38 | pow 0 = 1 |
| @@ -70,7 +70,7 @@ f z k = g z k h | |||
| 70 | -- actual function g (converts Int to Integer for more precision) | 70 | -- actual function g (converts Int to Integer for more precision) |
| 71 | g :: Int -> Int -> (Integer -> Integer -> Float) -> Float | 71 | g :: Int -> Int -> (Integer -> Integer -> Float) -> Float |
| 72 | g z k h = sum $ map (h $ fromIntegral z) [0..(fromIntegral k)] | 72 | g z k h = sum $ map (h $ fromIntegral z) [0..(fromIntegral k)] |
| 73 | 73 | ||
| 74 | -- helper function h using mem-table for the power-series (z^i) and for factorial (i!) | 74 | -- helper function h using mem-table for the power-series (z^i) and for factorial (i!) |
| 75 | hMT :: Integer -> Integer -> Float | 75 | hMT :: Integer -> Integer -> Float |
| 76 | hMT z i = (fromInteger $ pofNs z !! (fromInteger i)) / (fromInteger $ facs !! (fromInteger i)) | 76 | hMT z i = (fromInteger $ pofNs z !! (fromInteger i)) / (fromInteger $ facs !! (fromInteger i)) |
| @@ -86,15 +86,15 @@ h z i = (fromInteger $ z^i) / (fromInteger $ fac i) | |||
| 86 | -- gets the digits of an integer as a list | 86 | -- gets the digits of an integer as a list |
| 87 | digits :: Integer -> [Integer] | 87 | digits :: Integer -> [Integer] |
| 88 | digits x | 88 | digits x |
| 89 | | x<=0 = [] | 89 | | x<=0 = [] |
| 90 | | otherwise = (digits $ x `div` 10)++[x `mod` 10] | 90 | | otherwise = (digits $ x `div` 10)++[x `mod` 10] |
| 91 | 91 | ||
| 92 | -- calculates the goedel-number for the given integer | 92 | -- calculates the goedel-number for the given integer |
| 93 | -- returns 0 for non-positive numbers | 93 | -- returns 0 for non-positive numbers |
| 94 | gz :: Integer -> Integer | 94 | gz :: Integer -> Integer |
| 95 | gz n | 95 | gz n |
| 96 | | n<=0 = 0 | 96 | | n<=0 = 0 |
| 97 | | otherwise = product $ zipWith (^) primes (digits n) | 97 | | otherwise = product $ zipWith (^) primes (digits n) |
| 98 | 98 | ||
| 99 | -- goedel-number generator | 99 | -- goedel-number generator |
| 100 | gzs :: [Integer] | 100 | gzs :: [Integer] |
diff --git a/TestAufgabeFFP2.hs b/TestAufgabeFFP2.hs index 61fdb51..ef6d48a 100644 --- a/TestAufgabeFFP2.hs +++ b/TestAufgabeFFP2.hs | |||
| @@ -4,38 +4,38 @@ import Test.HUnit | |||
| 4 | import Control.Monad | 4 | import Control.Monad |
| 5 | import AufgabeFFP2 | 5 | import AufgabeFFP2 |
| 6 | 6 | ||
| 7 | 7 | cases1 = TestLabel "pps" $ TestList [ | |
| 8 | cases1 = TestLabel "2.1" $ TestList [ | 8 | TestCase $ assertEqual "take 10 pps" |
| 9 | TestCase $ | 9 | [(3,5),(5,7),(11,13),(17,19),(29,31),(41,43), (59,61),(71,73),(101,103),(107,109)] |
| 10 | [(3,5),(5,7),(11,13),(17,19),(29,31),(41,43), (59,61),(71,73),(101,103),(107,109)] | 10 | (take 10 pps), |
| 11 | @=? (take 10 pps), | 11 | TestCase $ assertEqual "pps!!20" (347,349) (pps!!20), |
| 12 | TestCase $ (347,349) @=? (pps!!20), | 12 | TestCase $ assertEqual "head (drop 30 pps)" (809,811) (head (drop 30 pps)) |
| 13 | TestCase $ (809,811) @=? (head (drop 30 pps)) | 13 | ] |
| 14 | ] | 14 | |
| 15 | 15 | ||
| 16 | 16 | cases2 = TestLabel "powFast" $ TestList [ | |
| 17 | cases2 = TestLabel "2.2" $ TestList [ | 17 | TestCase $ assertEqual "powFast 10" 1024 (powFast 10), |
| 18 | TestCase $ 1024 @=? (powFast 10), | 18 | TestCase $ assertEqual "powFast 10" (pow 10) (powFast 10), |
| 19 | TestCase $ (pow 10) @=? (powFast 10), | 19 | TestCase $ assertEqual "powFast 20" 1048576 (powFast 20), |
| 20 | TestCase $ 1048576 @=? (powFast 20), | 20 | TestCase $ assertEqual "powFast 20" (pow 20) (powFast 20) |
| 21 | TestCase $ (pow 20) @=? (powFast 20) | 21 | ] |
| 22 | ] | 22 | |
| 23 | 23 | cases3 = TestLabel "fMT/f" $ TestList [ | |
| 24 | 24 | TestCase $ assertEqual "fMT/f 10 10" (fMT 10 10) (f 10 10), | |
| 25 | cases3 = TestLabel "2.3" $ TestList [ | 25 | TestCase $ assertEqual "fMT/f 50 10" (fMT 50 10) (f 50 10), |
| 26 | TestCase $ (fMT 10 10) @=? (f 10 10), | 26 | TestCase $ assertEqual "fMT/f 1000 10" (fMT 1000 10) (f 1000 10) |
| 27 | TestCase $ (fMT 50 10) @=? (f 50 10), | 27 | ] |
| 28 | TestCase $ (fMT 1000 10) @=? (f 1000 10) | 28 | |
| 29 | ] | 29 | cases4 = TestLabel "gz/gzs" $ TestList [ |
| 30 | 30 | TestCase $ assertEqual "gz 42" 144 (gz 42), | |
| 31 | 31 | TestCase $ assertEqual "gz 402" 400 (gz 402), | |
| 32 | cases4 = TestLabel "2.4" $ TestList [ | 32 | TestCase $ assertEqual "gzs!!42" 144 (gzs!!42), |
| 33 | TestCase $ 144 @=? (gz 42), | 33 | TestCase $ assertEqual "gzs!!402" 400 (gzs!!402) |
| 34 | TestCase $ 400 @=? (gz 402), | 34 | ] |
| 35 | TestCase $ 144 @=? (gzs!!42), | 35 | |
| 36 | TestCase $ 400 @=? (gzs!!402) | 36 | tests :: [Test] |
| 37 | ] | 37 | tests = [cases1, cases2, cases3, cases4] |
| 38 | 38 | ||
| 39 | 39 | main = do | |
| 40 | 40 | forM tests $ \test -> | |
| 41 | main = runTestTT $ TestList [cases1, cases2, cases3, cases4] | 41 | runTestTT test |
