Created
January 19, 2021 03:13
-
-
Save alogic0/8065e2d7064057112ed3a1330f4785e1 to your computer and use it in GitHub Desktop.
Solution for math problem
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- Haskell version of formulae from | |
-- https://youtu.be/4FNCIYD8HdA | |
import Control.Monad (join) | |
import Data.Ratio | |
oneS :: [Rational] | |
oneS = join $ repeat [1, (-1)] | |
pSum :: Int -> Rational | |
pSum 1 = 1 | |
pSum 2 = 2 | |
pSum 3 = 3 | |
pSum n = sum (zipWith (*) oneS (map (\k -> pSum (n - k) * eSum k) [1 .. (n - 1)])) + fromIntegral n * (oneS !! n) * eSum n | |
eSum :: Int -> Rational | |
eSum 0 = 1 | |
eSum 1 = 1 | |
eSum n | n > 3 = 0 | |
eSum n = sum (zipWith (*) oneS (map (\k -> pSum k * eSum (n - k)) [1 .. n])) / fromIntegral n | |
-- now try | |
-- pSum 4 | |
-- pSum 5 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment