Fibonacci

import org.junit.Test import kotlin.test.assertEquals class FibonacciTest { @Test fun `fib first numbers`() { assertEquals(1, fib(0)) assertEquals(1, fib(1)) assertEquals(2, fib(2)) assertEquals(3, fib(3)) assertEquals(5, fib(4)) assertEquals(8, fib(5)) assertEquals(13, fib(6)) assertEquals(21, fib(7)) assertEquals(34, fib(8)) assertEquals(55, fib(9)) assertEquals(89, fib(10)) } } // Your code starts here //sampleStart fun fib(n: Int): Int = TODO() //sampleEnd
Save Restore Add own tests Switch to main Feedback

Define a function to calculate Fibonacci number on n-th position. It should start from 1 and 1 (fib(0) == 1, fib(1) == 1, fib(2) == 2, fib(3) == 3, fib(4) == 5, fib(5) == 8). More on Fibonacci sequence here. Consider both recursive and iterative solutions.


Previous
Next