summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authormanuel <manuel@mausz.at>2012-04-14 18:52:18 +0200
committermanuel <manuel@mausz.at>2012-04-14 18:52:18 +0200
commit9545fb945e42151afe04a384cb7acf43c1ab9350 (patch)
tree50da8a07ced88ba39ac515e6ff9d6508bee1b2e5
parent9a0159cc0ff165f33527c9a4fe1a8ac0a2afc15b (diff)
downloadffp-9545fb945e42151afe04a384cb7acf43c1ab9350.tar.gz
ffp-9545fb945e42151afe04a384cb7acf43c1ab9350.tar.bz2
ffp-9545fb945e42151afe04a384cb7acf43c1ab9350.zip
* remove trailing whitespaces
* fix wrong indention (no tabs, use 2 spaces, don't mix) * name testcaselists by their methods * add (more or less) individual testname per testcase * call testcase lists in a do-loop so that every testcase list generates a seperate output
-rw-r--r--AufgabeFFP2.hs36
-rw-r--r--TestAufgabeFFP2.hs70
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
7isPrime :: Integer -> Bool 7isPrime :: Integer -> Bool
8isPrime n = n > 1 && 8isPrime 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
19primes :: [Integer] 19primes :: [Integer]
20primes = 2:filter isPrime [3,5..] 20primes = 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
25pps :: [(Integer, Integer)] 25pps :: [(Integer, Integer)]
26pps = filter (\(_,x) -> isPrime x) $ map (\p -> (p,p+2)) primes 26pps = 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
33pof2s :: [Integer] 33pof2s :: [Integer]
34pof2s = [1] ++ map (2*) pof2s 34pof2s = [1] ++ map (2*) pof2s
35 35
36-- calculates 2^n 36-- calculates 2^n
37pow :: Int -> Integer 37pow :: Int -> Integer
38pow 0 = 1 38pow 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)
71g :: Int -> Int -> (Integer -> Integer -> Float) -> Float 71g :: Int -> Int -> (Integer -> Integer -> Float) -> Float
72g z k h = sum $ map (h $ fromIntegral z) [0..(fromIntegral k)] 72g 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!)
75hMT :: Integer -> Integer -> Float 75hMT :: Integer -> Integer -> Float
76hMT z i = (fromInteger $ pofNs z !! (fromInteger i)) / (fromInteger $ facs !! (fromInteger i)) 76hMT 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
87digits :: Integer -> [Integer] 87digits :: Integer -> [Integer]
88digits x 88digits 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
94gz :: Integer -> Integer 94gz :: Integer -> Integer
95gz n 95gz 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
100gzs :: [Integer] 100gzs :: [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
4import Control.Monad 4import Control.Monad
5import AufgabeFFP2 5import AufgabeFFP2
6 6
7 7cases1 = TestLabel "pps" $ TestList [
8cases1 = 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 16cases2 = TestLabel "powFast" $ TestList [
17cases2 = 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 23cases3 = TestLabel "fMT/f" $ TestList [
24 24 TestCase $ assertEqual "fMT/f 10 10" (fMT 10 10) (f 10 10),
25cases3 = 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 ] 29cases4 = TestLabel "gz/gzs" $ TestList [
30 30 TestCase $ assertEqual "gz 42" 144 (gz 42),
31 31 TestCase $ assertEqual "gz 402" 400 (gz 402),
32cases4 = 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) 36tests :: [Test]
37 ] 37tests = [cases1, cases2, cases3, cases4]
38 38
39 39main = do
40 40 forM tests $ \test ->
41main = runTestTT $ TestList [cases1, cases2, cases3, cases4] 41 runTestTT test