# Find nth Fibonacci number

0

0

The code in this package example defines a learning user function called fabonaccii(). This function takes an integer input argument, n, and returns an integer value for the function call. The fabonaccii() function first defines a variable called loop(), which will be used to hold a sequence of integers. The function loops through the input argument, x, counting from 1 up to but not including n. If x is greater than n, then the function returns the value of Last, which is the value of x + 1. If x is equal to n, then the function returns the value of NextLast, which is the value of x + Last. Finally, the function assigns the result of the loop() function to the variable, fabonaccii(). The value of fabonaccii() will be the sum of the values of 1, 2, and 3.

``````package example

object learning extends App {
println(s"hello world")

def fabonaccii(n:Int): Int = {

def loop(x: Int, Last: Int, NextLast: Int): Int = {

if(x >= n ) return Last
else {
// println(s" \$x - \$Last- \$NextLast")
return loop(x+1, Last + NextLast, Last)
}
}

loop(2,1,1)
}

println(fabonaccii(5))
}

/*
fabonaccii(n) =
fabonaccii(5)

1 1 2 3 5

*/``````
• Rulesets
• Playground
• Snippets
• Cookbooks
###### Legal

We are SOC-2 Compliance Certified