Add all the A tasks

This commit is contained in:
Ingy döt Net 2013-04-10 14:58:50 -07:00
parent 2dd7375f96
commit 051504d65b
1608 changed files with 18584 additions and 0 deletions

View file

@ -0,0 +1,17 @@
-- Return an approximation to the arithmetic-geometric mean of two numbers.
-- The result is considered accurate when two successive approximations are
-- sufficiently close, as determined by "eq".
agm :: (Floating a) => a -> a -> ((a, a) -> Bool) -> a
agm a g eq = snd . head . dropWhile (not . eq) $ iterate step (a, g)
where step (a, g) = ((a + g) / 2, sqrt (a * g))
-- Return the relative difference of the pair. We assume that at least one of
-- the values is far enough from 0 to not cause problems.
relDiff :: (Fractional a) => (a, a) -> a
relDiff (x, y) = let n = abs (x - y)
d = ((abs x) + (abs y)) / 2
in n / d
main = do
let equal = (< 0.000000001) . relDiff
print $ agm 1 (1 / sqrt 2) equal