Last active
January 15, 2017 23:01
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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