Hackerrank – Evaluating e^x
Popis problému Celý popis zadania sa nacháza – Hackerrank. Riešenie
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
object EvaluatingEx extends App { def f(x: Double): Double = { f2(x, 9) } def fact(x: Int): Int = if (x <= 1) 1 else x * fact(x - 1) def f2(x: Double, i: Int): Double = { if (i == 0) 1 else Math.pow(x, i) / fact(i) + f2(x, i - 1) } val n = readInt (1 to n) foreach (x => println(f(readFloat()))) } |
Riešenie tiež dostupné na mojom GitHub profile.