-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy patheval-ex.hs
36 lines (27 loc) · 875 Bytes
/
eval-ex.hs
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
{-
Evaluating e^x (https://www.hackerrank.com/challenges/eval-ex)
FP/Introduction, Easy, 20 pts
The series expansion of e^x is given by:
1 + x + x^2/2! + x^3/3! + x^4/4! + ...
Evaluate e^x for given values of x by using the above expansion for the first 10 terms.
Input: N, then N lines with values for x.
Output: N lines with evaluated e^x, for each x from input.
-}
import Control.Monad
import Text.Printf
factorial :: Integral a => a -> a
factorial 0 = 1
factorial n = n * factorial (n-1)
pow :: (Num a, Integral b) => a -> b -> a
pow _ 0 = 1
pow x n = x * pow x (n - 1)
--exp10 :: a -> a
exp10 x = sum [pow x i / fromIntegral (factorial i) | i <- [0..9]]
main :: IO ()
main = do
n_temp <- getLine
let n = read n_temp :: Int
forM_ [1..n] $ \a0 -> do
x_temp <- getLine
let x = read x_temp :: Double
printf "%.4f\n" (exp10 x)