From 12ad8b53c5be7e22975082baa715e96aa2f1b9dd Mon Sep 17 00:00:00 2001 From: Matthias Wisniowski Date: Sun, 25 Mar 2012 19:04:12 +0200 Subject: 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. --- AufgabeFFP2.hs | 38 +++++++++++++++++++++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-) 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 powFast 0 = 1 powFast n = pof2s !! (n-1) + pof2s !! (n-1) -------------------------------------------------------------------------------- \ No newline at end of file +------------------------------------------------------------------------------- + +-- 3 + +-- power series of N +pofNs :: Integer -> [Integer] +pofNs n = [1] ++ (map (n*) $ pofNs n) + +-- faculty function +fac :: Integer -> Integer +fac n = product [1..n] + +-- stream of faculties +facGen :: [Integer] +facGen = map (fac) [1..] + +-- function g with memoization (using hFast) +fMT :: Int -> Int -> Float +fMT z k = g z k hFast + +-- function g without memoization (uning hSlow) +f :: Int -> Int -> Float +f z k = g z k hSlow + +-- actual function g +g :: Int -> Int -> (Int -> Int -> Float) -> Float +g z k h = sum $ map (h z) [1..k] + +-- helper function h using mem-table for the power-series (z^i) and for faculty (i!) +hFast :: Int -> Int -> Float +hFast z i = (fromInteger $ pofNs (fromIntegral z) !! i) / (fromInteger $ facGen !! (i-1)) + +-- helper function h without memoization +hSlow :: Int -> Int -> Float +hSlow z i = (fromIntegral $ z^i) / (fromInteger $ fac $ fromIntegral i) + +------------------------------------------------------------------------------- -- cgit v1.2.3