diff options
| author | Matthias Wisniowski <matthias.wisniowski@gmail.com> | 2012-03-25 19:04:12 +0200 |
|---|---|---|
| committer | Matthias Wisniowski <matthias.wisniowski@gmail.com> | 2012-03-25 19:04:12 +0200 |
| commit | 12ad8b53c5be7e22975082baa715e96aa2f1b9dd (patch) | |
| tree | 9e7de4b49d08cabc7b88b9c7927f4b8e9108e23d | |
| parent | d0bf96ede7e199aac494e65a239edb9d8bf9f413 (diff) | |
| download | ffp-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.hs | 38 |
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 | |||
| 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 hFast | ||
| 60 | |||
| 61 | -- function g without memoization (uning hSlow) | ||
| 62 | f :: Int -> Int -> Float | ||
| 63 | f z k = g z k hSlow | ||
| 64 | |||
| 65 | -- actual function g | ||
| 66 | g :: Int -> Int -> (Int -> Int -> Float) -> Float | ||
| 67 | g 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!) | ||
| 70 | hFast :: Int -> Int -> Float | ||
| 71 | hFast z i = (fromInteger $ pofNs (fromIntegral z) !! i) / (fromInteger $ facGen !! (i-1)) | ||
| 72 | |||
| 73 | -- helper function h without memoization | ||
| 74 | hSlow :: Int -> Int -> Float | ||
| 75 | hSlow z i = (fromIntegral $ z^i) / (fromInteger $ fac $ fromIntegral i) | ||
| 76 | |||
| 77 | ------------------------------------------------------------------------------- | ||
