Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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