Skip to content

Instantly share code, notes, and snippets.

@markroxor
Last active August 27, 2020 17:29
Show Gist options
  • Select an option

  • Save markroxor/aed9af671ea18364a9c31f24e1522c8c to your computer and use it in GitHub Desktop.

Select an option

Save markroxor/aed9af671ea18364a9c31f24e1522c8c to your computer and use it in GitHub Desktop.
import Text.Printf (printf)
integrate l = [(n,p+1,(p+1)*d) | (n,p,d) <- l]
sq l = [(n*n', p+p', d*d') | (n,p,d) <- l, (n',p',d') <- l ]
solve_ l v = sum $ [( fromIntegral n)*(( fromIntegral v)^p) / (fromIntegral d) | (n, p, d) <- l]
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