We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
object Solution {
def main(args: Array[String]):Unit = {
val stdin = scala.io.StdIn
val n = stdin.readLine.trim.toInt
for (nItr <- 1 to n) {
val x = stdin.readLine.trim.toDouble
println(((make(x) * 10000).round).toDouble / 10000.toDouble)
}
}
Evaluating e^x
You are viewing a single comment's thread. Return to all comments →
My working scala code :
object Solution { def main(args: Array[String]):Unit = { val stdin = scala.io.StdIn val n = stdin.readLine.trim.toInt for (nItr <- 1 to n) { val x = stdin.readLine.trim.toDouble println(((make(x) * 10000).round).toDouble / 10000.toDouble) } }
def sum(a: Double, b: Double): Double = return a + b;
def make(x: Double): Double = { return Range(0, 10).map(number => Math.pow(x, number) / factorial(number)).sum
}
def factorial(x: Double): Double = { if (x == 0) return 1 else return x * factorial(x - 1) } }