Problem Statement
A description of the problem can be found on Hackerrank.
Solution
Use the equation for Fibonacci numbers in problem statement:
Fibonacci(n) = 0 , n = 1
Fibonacci(n) = 1 , n = 2
Fibonacci(n) = Fibonacci(n-1) + Fibonacci(n-2) , n > 2
I created solution in:
All solutions are also available on my GitHub.
Scala
1 2 3 4 5 6 7 8 9 10 11 |
object FibonacciNumbers { def fibonacci(x:Int):Int = { if(x == 1) 0 else if(x == 2) 1 else fibonacci(x - 1) + fibonacci(x - 2) } def main(args: Array[String]) { println(fibonacci(readInt())) } } |