Last active
August 27, 2020 17:29
-
-
Save markroxor/aed9af671ea18364a9c31f24e1522c8c to your computer and use it in GitHub Desktop.
This file contains 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
import Text.Printf (printf) | |
-- (n,p,d) represents coefficient, power, denominator of a algebric term. | |
-- (4,2,3) == 4 * (x ^ 2) /3 | |
-- f(x) == n1 * (x ^ p1) / d1 + n2 * (x ^ p2) / d2 .... | |
-- integrates f(x) | |
integrate l = [(n,p+1,(p+1)*d) | (n,p,d) <- l] | |
-- squares a function - f^2(x) | |
-- f(x) = (x+1) | |
-- f^2(x) = (x+1)^2 | |
sq l = [(n*n', p+p', d*d') | (n,p,d) <- l, (n',p',d') <- l ] | |
-- evaluates the expr for a value | |
solve_ l v = sum ([( fromIntegral n)*(( fromIntegral v)^p) / (fromIntegral d) | (n, p, d) <- l, p >= 0]) + ( sum [( fromIntegral n)/(( fromIntegral v)^(-p) * (fromIntegral d)) | (n, p, d) <- l, p < 0]) | |
solve :: Int -> Int -> [Int] -> [Int] -> [Double] | |
solve l r a b | |
| otherwise = [area, vol] | |
where | |
c = [1,1 ..] | |
f = zip3 a b c | |
f_area = integrate f | |
f_volume = integrate $ sq f | |
area = (solve_ f_area r) - (solve_ f_area l) | |
vol = pi * ((solve_ f_volume r) - (solve_ f_volume l)) | |
main :: IO () | |
main = getContents >>= mapM_ (printf "%.1f\n"). (\[a, b, [l, r]] -> solve l r a b). map (map read. words). lines |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment