-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy patheval-ex.hs
More file actions
38 lines (33 loc) · 993 Bytes
/
eval-ex.hs
File metadata and controls
38 lines (33 loc) · 993 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
-- Author: btjanaka (Bryon Tjanaka)
-- Problem: (HackerRank) eval-ex
-- Title: Evaluating e^x
-- Link: https://www.hackerrank.com/challenges/eval-ex/problem
-- Idea: (implementation)
-- Difficulty: easy
-- Tags: fp
{-# LANGUAGE FlexibleInstances, UndecidableInstances,
DuplicateRecordFields #-}
module Main where
import Control.Monad
import Data.Array
import Data.Bits
import Data.List
import Data.List.Split
import Data.Set
import Debug.Trace
import System.Environment
import System.IO
import System.IO.Unsafe
factorial :: Integer -> Integer
factorial n | n <= 1 = 1
| otherwise = n * (factorial $ n - 1)
e :: Double -> Int -> Double
e x 0 = 1
e x n =
x ** (fromIntegral n) / (fromIntegral $ factorial $ toInteger n) + e x (n - 1)
main :: IO ()
main = do
n <- readLn :: IO Int
forM_ [1 .. n] $ \n_itr -> do
x <- readLn :: IO Double
putStrLn $ show $ e x 9