diff options
| author | Matthias Wisniowski <matthias.wisniowski@gmail.com> | 2012-03-26 00:21:42 +0200 |
|---|---|---|
| committer | Matthias Wisniowski <matthias.wisniowski@gmail.com> | 2012-03-26 00:21:42 +0200 |
| commit | 3079424b11b32d019a68f13299e372a6aa277163 (patch) | |
| tree | e07b8b77378d6bd99ac83f7615c1fac775419008 | |
| parent | d0bf96ede7e199aac494e65a239edb9d8bf9f413 (diff) | |
| parent | 8613f7154f815835fc5e6ddbe15e9aebc62109b1 (diff) | |
| download | ffp-3079424b11b32d019a68f13299e372a6aa277163.tar.gz ffp-3079424b11b32d019a68f13299e372a6aa277163.tar.bz2 ffp-3079424b11b32d019a68f13299e372a6aa277163.zip | |
Merge branch 'dev'
| -rw-r--r-- | AufgabeFFP2.hs | 57 | ||||
| -rw-r--r-- | TestAufgabeFFP2.hs | 69 |
2 files changed, 125 insertions, 1 deletions
diff --git a/AufgabeFFP2.hs b/AufgabeFFP2.hs index e66b7a8..7993c01 100644 --- a/AufgabeFFP2.hs +++ b/AufgabeFFP2.hs | |||
| @@ -38,4 +38,59 @@ powFast :: Int -> Integer | |||
| 38 | powFast 0 = 1 | 38 | powFast 0 = 1 |
| 39 | powFast n = pof2s !! (n-1) + pof2s !! (n-1) | 39 | powFast n = pof2s !! (n-1) + pof2s !! (n-1) |
| 40 | 40 | ||
| 41 | ------------------------------------------------------------------------------- \ No newline at end of file | 41 | ------------------------------------------------------------------------------- |
| 42 | |||
| 43 | -- 3 | ||
| 44 | |||
| 45 | -- power series of N | ||
| 46 | pofNs :: Integer -> [Integer] | ||
| 47 | pofNs n = [1] ++ (map (n*) $ pofNs n) | ||
| 48 | |||
| 49 | -- faculty function | ||
| 50 | fac :: Integer -> Integer | ||
| 51 | fac n = product [1..n] | ||
| 52 | |||
| 53 | -- stream of faculties | ||
| 54 | facGen :: [Integer] | ||
| 55 | facGen = map (fac) [1..] | ||
| 56 | |||
| 57 | -- function g with memoization (using hFast) | ||
| 58 | fMT :: Int -> Int -> Float | ||
| 59 | fMT z k = g z k hMT | ||
| 60 | |||
| 61 | -- function g without memoization (uning hSlow) | ||
| 62 | f :: Int -> Int -> Float | ||
| 63 | f z k = g z k h | ||
| 64 | |||
| 65 | -- actual function g (converts Int to Integer for more precision) | ||
| 66 | g :: Int -> Int -> (Integer -> Integer -> Float) -> Float | ||
| 67 | g z k h = sum $ map (h $ fromIntegral z) [1..(fromIntegral k)] | ||
| 68 | |||
| 69 | -- helper function h using mem-table for the power-series (z^i) and for faculty (i!) | ||
| 70 | hMT :: Integer -> Integer -> Float | ||
| 71 | hMT z i = (fromInteger $ pofNs z !! (fromInteger i)) / (fromInteger $ facGen !! ((fromIntegral i)-1)) | ||
| 72 | |||
| 73 | -- helper function h without memoization | ||
| 74 | h :: Integer -> Integer -> Float | ||
| 75 | h z i = (fromInteger $ z^i) / (fromInteger $ fac i) | ||
| 76 | |||
| 77 | ------------------------------------------------------------------------------- | ||
| 78 | |||
| 79 | -- 4 | ||
| 80 | |||
| 81 | -- gets the digits of an integer as a list | ||
| 82 | digits :: Integer -> [Integer] | ||
| 83 | digits x | ||
| 84 | | x<=0 = [] | ||
| 85 | | otherwise = (digits $ x `div` 10)++[x `mod` 10] | ||
| 86 | |||
| 87 | -- calculates the goedel-number for the given integer | ||
| 88 | -- returns 0 for non-positive numbers | ||
| 89 | gz :: Integer -> Integer | ||
| 90 | gz n | ||
| 91 | | n<=0 = 0 | ||
| 92 | | otherwise = product $ zipWith (^) primes (digits n) | ||
| 93 | |||
| 94 | -- goedel-number generator | ||
| 95 | gzs :: [Integer] | ||
| 96 | gzs = map (gz) [1..] | ||
diff --git a/TestAufgabeFFP2.hs b/TestAufgabeFFP2.hs new file mode 100644 index 0000000..0df8612 --- /dev/null +++ b/TestAufgabeFFP2.hs | |||
| @@ -0,0 +1,69 @@ | |||
| 1 | module Main where | ||
| 2 | |||
| 3 | import Test.HUnit | ||
| 4 | import Control.Monad | ||
| 5 | import AufgabeFFP2 | ||
| 6 | |||
| 7 | assertBoolF :: String -> Bool -> Assertion | ||
| 8 | assertBoolF msg b = when b (assertFailure msg) | ||
| 9 | |||
| 10 | ------------------------------------------------------------------------------- | ||
| 11 | -- 1 | ||
| 12 | |||
| 13 | pps1 :: Test | ||
| 14 | pps1 = TestCase (assertEqual "pps" | ||
| 15 | [(3,5),(5,7),(11,13),(17,19),(29,31),(41,43), (59,61),(71,73),(101,103),(107,109)] | ||
| 16 | (take 10 pps)) | ||
| 17 | |||
| 18 | pps2 :: Test | ||
| 19 | pps2 = TestCase (assertEqual "pps" | ||
| 20 | (347,349) | ||
| 21 | (pps!!20)) | ||
| 22 | |||
| 23 | pps3 :: Test | ||
| 24 | pps3 = TestCase (assertEqual "pps" | ||
| 25 | (809,811) | ||
| 26 | (head (drop 30 pps))) | ||
| 27 | |||
| 28 | ppsTests = TestList [pps1, pps2, pps3] | ||
| 29 | |||
| 30 | ------------------------------------------------------------------------------- | ||
| 31 | -- 2 | ||
| 32 | |||
| 33 | ------------------------------------------------------------------------------- | ||
| 34 | -- 3 | ||
| 35 | |||
| 36 | ------------------------------------------------------------------------------- | ||
| 37 | -- 4 | ||
| 38 | |||
| 39 | gz1 :: Test | ||
| 40 | gz1 = TestCase (assertEqual "gz" | ||
| 41 | 144 | ||
| 42 | (gz 42)) | ||
| 43 | |||
| 44 | gz2 :: Test | ||
| 45 | gz2 = TestCase (assertEqual "gz" | ||
| 46 | 400 | ||
| 47 | (gz 402)) | ||
| 48 | |||
| 49 | gzs1 :: Test | ||
| 50 | gzs1 = TestCase (assertEqual "gzs" | ||
| 51 | 144 | ||
| 52 | (gzs!!41)) | ||
| 53 | |||
| 54 | gzs2 :: Test | ||
| 55 | gzs2 = TestCase (assertEqual "gzs" | ||
| 56 | 400 | ||
| 57 | (gzs!!401)) | ||
| 58 | |||
| 59 | |||
| 60 | gzTests = TestList [gz1, gz2, gzs1, gzs2] | ||
| 61 | |||
| 62 | ------------------------------------------------------------------------------- | ||
| 63 | |||
| 64 | tests :: [Test] | ||
| 65 | tests = [ppsTests, gzTests] | ||
| 66 | |||
| 67 | main = do | ||
| 68 | forM tests $ \test -> | ||
| 69 | runTestTT test | ||
