Skip to content

Instantly share code, notes, and snippets.

@newlawrence
Last active February 19, 2017 23:32
Show Gist options
  • Save newlawrence/0c56180e9f753e60852a90059e4e964e to your computer and use it in GitHub Desktop.
Save newlawrence/0c56180e9f753e60852a90059e4e964e to your computer and use it in GitHub Desktop.
Computes the product of the first n elements of the Wallis succession
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