fibonacci series


See On Github

Data

Tags

recursion

Source Code

/* SCALA Implementation to generate fibonacci sereis upto N numbers. 
 
 	Though all the edge cases are covered, in case you need to check for something please do add
 	a test case for it.

 	Any improvement in the code is highly appreciated. 
*/

class GenerateFibonacci {

  def generateNthFibonacciTerm(num: Int): Int = {
    if (num < 2) num
    else generateNthFibonacciTerm(num - 1) + generateNthFibonacciTerm(num - 2)
  }

  def generateFibonacciSequence(num: Int): String = {
    val rangeList = (0 to num-1).toList
    val fiboList = rangeList.map(x=>generateNthFibonacciTerm(x))

    fiboList.mkString(" ")
  }
}
import org.scalatest.{Matchers, FunSuite}


class GenerateFibonacciTest extends FunSuite with Matchers {

  test("Fibonacci Sereis should generate") {
    val objectForFibonacci = new GenerateFibonacci

    objectForFibonacci.generateFibonacciSequence(1) should be("0")
    objectForFibonacci.generateFibonacciSequence(2) should be("0 1")
    objectForFibonacci.generateFibonacciSequence(5) should be("0 1 1 2 3")
    objectForFibonacci.generateFibonacciSequence(10) should be("0 1 1 2 3 5 8 13 21 34")
  }

}