• + 0 comments

    Here's the solution AS INTENDED with accumulators:

    import scala.io.StdIn.readInt
    
    object Solution {
        
        def fibonacci(x:Int):Int = {
            def loop(n: Int, two_away: Int, one_away: Int): Int = {
                if (n == 0) two_away+one_away else loop(n - 1, one_away, two_away+one_away)
            }
            if (x == 1) {
                return 0
            }
            if (x == 2) {
                return 1
            }
            loop(x-3, 0, 1)
         }
    
        def main(args: Array[String]) {
             /** This will handle the input and output**/
             println(fibonacci(readInt()))
    
        }
    }