diff options
| author | Matthias Wisniowski <matthias.wisniowski@gmail.com> | 2012-03-26 19:43:07 +0200 |
|---|---|---|
| committer | Matthias Wisniowski <matthias.wisniowski@gmail.com> | 2012-03-26 19:43:07 +0200 |
| commit | 4ae6d25fafe70ae372c6b5682b43cf10f2192ef9 (patch) | |
| tree | cccd7f67fb4493044ea24355ef48de985989407b | |
| parent | 96e23eedb94c95b2758db0733f8bb5d823908b87 (diff) | |
| download | ffp-4ae6d25fafe70ae372c6b5682b43cf10f2192ef9.tar.gz ffp-4ae6d25fafe70ae372c6b5682b43cf10f2192ef9.tar.bz2 ffp-4ae6d25fafe70ae372c6b5682b43cf10f2192ef9.zip | |
@2.3 Efficient factorial-generator
| -rw-r--r-- | AufgabeFFP2.hs | 22 |
1 files changed, 12 insertions, 10 deletions
diff --git a/AufgabeFFP2.hs b/AufgabeFFP2.hs index 44b2efb..0415b32 100644 --- a/AufgabeFFP2.hs +++ b/AufgabeFFP2.hs | |||
| @@ -19,10 +19,11 @@ isPrime n = n > 1 && | |||
| 19 | primes :: [Integer] | 19 | primes :: [Integer] |
| 20 | primes = 2:filter isPrime [3,5..] | 20 | primes = 2:filter isPrime [3,5..] |
| 21 | 21 | ||
| 22 | -- pairs of (p,p+2) | p,p+2 <- primes | ||
| 22 | -- 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 |
| 23 | -- pair is valid if the second component n is a prime | 24 | -- pair is valid if the second component n is a prime |
| 24 | pps :: [(Integer, Integer)] | 25 | pps :: [(Integer, Integer)] |
| 25 | pps = filter (\(_,y) -> isPrime y) $ map (\x -> (x,x+2)) primes | 26 | pps = filter (\(_,x) -> isPrime x) $ map (\p -> (p,p+2)) primes |
| 26 | 27 | ||
| 27 | ------------------------------------------------------------------------------- | 28 | ------------------------------------------------------------------------------- |
| 28 | 29 | ||
| @@ -32,11 +33,12 @@ pps = filter (\(_,y) -> isPrime y) $ map (\x -> (x,x+2)) primes | |||
| 32 | pof2s :: [Integer] | 33 | pof2s :: [Integer] |
| 33 | pof2s = [1] ++ map (2*) pof2s | 34 | pof2s = [1] ++ map (2*) pof2s |
| 34 | 35 | ||
| 36 | -- calculates 2^n | ||
| 35 | pow :: Int -> Integer | 37 | pow :: Int -> Integer |
| 36 | pow 0 = 1 | 38 | pow 0 = 1 |
| 37 | pow n = pow (n-1) + pow (n-1) | 39 | pow n = pow (n-1) + pow (n-1) |
| 38 | 40 | ||
| 39 | -- uses memtable for power-calculation | 41 | -- same as pow but uses memtable for power-calculation |
| 40 | powFast :: Int -> Integer | 42 | powFast :: Int -> Integer |
| 41 | powFast 0 = 1 | 43 | powFast 0 = 1 |
| 42 | powFast n = pof2s !! (n-1) + pof2s !! (n-1) | 44 | powFast n = pof2s !! (n-1) + pof2s !! (n-1) |
| @@ -49,14 +51,14 @@ powFast n = pof2s !! (n-1) + pof2s !! (n-1) | |||
| 49 | pofNs :: Integer -> [Integer] | 51 | pofNs :: Integer -> [Integer] |
| 50 | pofNs n = [1] ++ (map (n*) $ pofNs n) | 52 | pofNs n = [1] ++ (map (n*) $ pofNs n) |
| 51 | 53 | ||
| 52 | -- faculty function | 54 | -- stream of factorials |
| 55 | facs :: [Integer] | ||
| 56 | facs = scanl (*) 1 [1..] | ||
| 57 | |||
| 58 | -- factorial function | ||
| 53 | fac :: Integer -> Integer | 59 | fac :: Integer -> Integer |
| 54 | fac n = product [1..n] | 60 | fac n = product [1..n] |
| 55 | 61 | ||
| 56 | -- stream of faculties | ||
| 57 | facGen :: [Integer] | ||
| 58 | facGen = map (fac) [1..] | ||
| 59 | |||
| 60 | -- function g with memoization (using hFast) | 62 | -- function g with memoization (using hFast) |
| 61 | fMT :: Int -> Int -> Float | 63 | fMT :: Int -> Int -> Float |
| 62 | fMT z k = g z k hMT | 64 | fMT z k = g z k hMT |
| @@ -69,9 +71,9 @@ f z k = g z k h | |||
| 69 | g :: Int -> Int -> (Integer -> Integer -> Float) -> Float | 71 | g :: Int -> Int -> (Integer -> Integer -> Float) -> Float |
| 70 | g z k h = sum $ map (h $ fromIntegral z) [1..(fromIntegral k)] | 72 | g z k h = sum $ map (h $ fromIntegral z) [1..(fromIntegral k)] |
| 71 | 73 | ||
| 72 | -- helper function h using mem-table for the power-series (z^i) and for faculty (i!) | 74 | -- helper function h using mem-table for the power-series (z^i) and for factorial (i!) |
| 73 | hMT :: Integer -> Integer -> Float | 75 | hMT :: Integer -> Integer -> Float |
| 74 | hMT z i = (fromInteger $ pofNs z !! (fromInteger i)) / (fromInteger $ facGen !! ((fromIntegral i)-1)) | 76 | hMT z i = (fromInteger $ pofNs z !! (fromInteger i)) / (fromInteger $ facs !! (fromInteger i-1)) |
| 75 | 77 | ||
| 76 | -- helper function h without memoization | 78 | -- helper function h without memoization |
| 77 | h :: Integer -> Integer -> Float | 79 | h :: Integer -> Integer -> Float |
| @@ -96,4 +98,4 @@ gz n | |||
| 96 | 98 | ||
| 97 | -- goedel-number generator | 99 | -- goedel-number generator |
| 98 | gzs :: [Integer] | 100 | gzs :: [Integer] |
| 99 | gzs = map (gz) [1..] | 101 | gzs = map gz [1..] |
