Skip to content

Instantly share code, notes, and snippets.

@jgould22
Last active January 15, 2017 23:01
Show Gist options
  • Save jgould22/88acc25e0086730288c6fd207f1b6088 to your computer and use it in GitHub Desktop.
Save jgould22/88acc25e0086730288c6fd207f1b6088 to your computer and use it in GitHub Desktop.
A simple program to calculate a Fibonacci number up to fib number 1477
object fibobject {
def fib(n: Int): Double ={
@annotation.tailrec
def fibLocalFunc(n: Int, curr: Double,prev: Double): Double ={
if(n<=0) curr
else fibLocalFunc(n-1,curr+prev, curr)
}
if(n==1) 0
else if (n==2) 1
else fibLocalFunc(n-2,1,0)
}
def main (args: Array[String]): Unit = {
println("Enter a number:")
val input = scala.io.StdIn.readLine()
println(fib(input.toInt))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment