summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMatthias Wisniowski <matthias.wisniowski@gmail.com>2012-03-25 19:04:12 +0200
committerMatthias Wisniowski <matthias.wisniowski@gmail.com>2012-03-25 19:04:12 +0200
commit12ad8b53c5be7e22975082baa715e96aa2f1b9dd (patch)
tree9e7de4b49d08cabc7b88b9c7927f4b8e9108e23d
parentd0bf96ede7e199aac494e65a239edb9d8bf9f413 (diff)
downloadffp-12ad8b53c5be7e22975082baa715e96aa2f1b9dd.tar.gz
ffp-12ad8b53c5be7e22975082baa715e96aa2f1b9dd.tar.bz2
ffp-12ad8b53c5be7e22975082baa715e96aa2f1b9dd.zip
First implementation for 2.3
hFast and hSlow diverge in values, reason unknown. e.g. consider >> map (\x -> hFast x x) [1..15] << (similar for hSlow) hSlow has the wrong values after 10.
-rw-r--r--AufgabeFFP2.hs38
1 files changed, 37 insertions, 1 deletions
diff --git a/AufgabeFFP2.hs b/AufgabeFFP2.hs
index e66b7a8..e4e646b 100644
--- a/AufgabeFFP2.hs
+++ b/AufgabeFFP2.hs
@@ -38,4 +38,40 @@ powFast :: Int -> Integer
38powFast 0 = 1 38powFast 0 = 1
39powFast n = pof2s !! (n-1) + pof2s !! (n-1) 39powFast 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
46pofNs :: Integer -> [Integer]
47pofNs n = [1] ++ (map (n*) $ pofNs n)
48
49-- faculty function
50fac :: Integer -> Integer
51fac n = product [1..n]
52
53-- stream of faculties
54facGen :: [Integer]
55facGen = map (fac) [1..]
56
57-- function g with memoization (using hFast)
58fMT :: Int -> Int -> Float
59fMT z k = g z k hFast
60
61-- function g without memoization (uning hSlow)
62f :: Int -> Int -> Float
63f z k = g z k hSlow
64
65-- actual function g
66g :: Int -> Int -> (Int -> Int -> Float) -> Float
67g z k h = sum $ map (h z) [1..k]
68
69-- helper function h using mem-table for the power-series (z^i) and for faculty (i!)
70hFast :: Int -> Int -> Float
71hFast z i = (fromInteger $ pofNs (fromIntegral z) !! i) / (fromInteger $ facGen !! (i-1))
72
73-- helper function h without memoization
74hSlow :: Int -> Int -> Float
75hSlow z i = (fromIntegral $ z^i) / (fromInteger $ fac $ fromIntegral i)
76
77-------------------------------------------------------------------------------