Question: The aim of the application is to display a number sequence and the user has to choose the sixth digit:

`1 2 3 5 8 ...`

Although [this Q&A](https://stackoverflow.com/questions/7388416/what-is-the-fastest-way-to-write-fibonacci-function-in-scala) has been checked, another code had to be created in order to meet the demands of the application, namely returning the sixth digit of the number sequence.

**Test**

```
import org.scalatest.FunSuite
import org.scalatest.FunSpec 
import org.scalatest.matchers.ShouldMatchers

class CalculationTest extends FunSuite {
  test("fibonacci") {
    assert(calculation.fibonacci(1, 2) === 13)
  } 
}

```

**Main**

```
object Calculation {
  def fibonacci(a: Int, b: Int) : Int = {
    var first:Int = a
    var second:Int = b
    var third:Int = 0
    var fourth:Int = 0
    var fifth:Int = 0
    var sixth:Int = 0

    third = first + second
    fourth = second + third
    fifth = third + fourth
    sixth = fourth + fifth

    return sixth
  }
}

```

Is it possible to simplify the main code, e.g. just returning the sixth digit instead of calculating each individual digit?

Answer: 
You can define the sequence as a infinite lazy stream of ints. They won't be calculated until requested:

```
scala> lazy val fibs: Stream[Int] = 1 #:: fibs.scanLeft(2)(_+_)
fibs: Stream[Int] = <lazy>

```

then you can ask for just the 6th item, which forces the calculation of just the first 6 items:

```
scala> fibs(5)
res0: Int = 13

```

or you can ask for the first 6:

```
scala> fibs.take(6)
res1: scala.collection.immutable.Stream[Int] = Stream(1, ?)

```

but note that it is still acting lazy, you can force them to be calculated:

```
scala> fibs.take(6).force
res2: scala.collection.immutable.Stream[Int] = Stream(1, 2, 3, 5, 8, 13)

```

or just get your results in a list:

```
scala> fibs.take(6).toList
res3: List[Int] = List(1, 2, 3, 5, 8, 13)

```
I would go for the most general approach:

```
class Calculation(private var a : Int, private var b : Int) {
  private def fibCalc(n : Int) : Int =
    if(n == 0) a
    else if(n == 1) b
    else if(n > 1) fibCalc(n-1) + fibCalc(n-2)

  def fibonacci = fibCalc(6)
}

```

This makes changing it from the 6th to the nth for some arbitrary `n` a much easier task. (The standard recursive solution is obviously not optimal, although it doesn't really matter for n = 6. There are other methods you could try, like the matrix exponentiation implementation).