-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathFibonacciRec.scala
46 lines (45 loc) · 1.16 KB
/
FibonacciRec.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import scala.annotation.tailrec
object Fibonacci extends App{
private def fib(n:Int):Int ={
if (n <= 0)
0
else if (n == 1)
1
else
fib(n-1) + fib(n-2)
}
def fibTail(x:Int):BigInt = {
@tailrec
def fib(x:Int, prev:BigInt = 0, next:BigInt =1) : BigInt = x match{
case 0 =>prev
case 1 => next
case _ => fib(x-1, next, (next + prev))
}
fib(x)
}
def fibIterative(n:Int):BigInt = {
var (fibPrev,fib,index) = (0,1,1)
//Need n-1 times more
for( index <- 2 to n){
var temp = fib
fib+= fibPrev
fibPrev = temp
}
fib
}
def fibMemo(n:Int, memo:Array[Int]):Int={
if(n<=0)
return 0
else if(n==1)
return 1
else if(memo(n)==0){
memo(n)= fibMemo(n-1,memo) + fibMemo(n-2, memo)
}
memo(n)
}
println(fib(10))
println(fibIterative(10))
println(fibTail(10))
val memo = new Array[Int](11)
println(fibMemo(10,memo))
}