Skip to content

Instantly share code, notes, and snippets.

@alogic0
Created January 19, 2021 03:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alogic0/8065e2d7064057112ed3a1330f4785e1 to your computer and use it in GitHub Desktop.
Save alogic0/8065e2d7064057112ed3a1330f4785e1 to your computer and use it in GitHub Desktop.
Solution for math problem
-- 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