Last active
February 19, 2017 23:32
-
-
Save newlawrence/0c56180e9f753e60852a90059e4e964e to your computer and use it in GitHub Desktop.
Computes the product of the first n elements of the Wallis succession
This file contains hidden or 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 Data.Ratio | |
import Text.Read | |
import System.Environment | |
wallis_succession :: [Rational] | |
wallis_succession = merge odds evens | |
where | |
odds = [n % (n - 1) | n <- [2, 4..]] | |
evens = [n % (n + 1) | n <- [2, 4..]] | |
merge (x:xs) (y:ys) = x : y : merge xs ys | |
wallisProduct :: Int -> Rational | |
wallisProduct n = product $ take n wallis_succession | |
main = do | |
args <- getArgs | |
let n = do | |
(n:_) <- Just args | |
readMaybe n | |
case n of | |
Nothing -> putStrLn "Invalid input" | |
Just n -> do | |
putStrLn $ "Wallis product for " ++ (show n) ++ " elements" | |
putStrLn $ "Fraction: " ++ (show . wallisProduct $ n) | |
putStrLn $ "Value: " ++ (show . fromRational . wallisProduct $ n) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment