-1

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 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?

Community
  • 1
  • 1
030
  • 10,842
  • 12
  • 78
  • 123
  • I'm not sure what your question is. Do you want something that only calculates the standard fibonnacci sequence, or a variety of different sequences? Are you just trying to simplify the code for calculating the sequences? – DNA Jul 26 '14 at 22:01
  • Question has been updated – 030 Jul 26 '14 at 22:05
  • 2
    why the hell do you need a class if you can just pass the same args to a function? brainwashed by Java? – Erik Kaplun Jul 27 '14 at 02:23
  • @ErikAllik The class has been removed, but now the following issue occurs while running the test `[error] expected class or object definition\n[error] def addition(private var a: Int, private var b: Int) = a + b` – 030 Jul 27 '14 at 10:27
  • @ErikAllik Question has been updated. Arguments are now passed to function directly instead via Class. Class changed into Object. Ran the test again and it works indeed. – 030 Jul 27 '14 at 12:43

3 Answers3

4

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)
stew
  • 11,276
  • 36
  • 49
2

Your code could be reduced to this without much effort. It still doesn't make a lot of sense to me though. Important point: you don't need vars at all.

class Calculation(val a: Int, val b: Int) {
  def fibonacci() : Int = {
    val third = a + b
    val fourth = b + third
    val fifth = third + fourth
    fourth + fifth
  }  
}
Diego Basch
  • 12,764
  • 2
  • 29
  • 24
0

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).

amnn
  • 3,657
  • 17
  • 23