Skip to content

Commit c5a6168

Browse files
authoredOct 7, 2020
Merge pull request #1072 from jhoffman222/dynamicFibonacciKotlin
Show Kotlin dynamic programming fibonacci number
2 parents c88de69 + 2309378 commit c5a6168

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
object Fibonacci {
2+
@JvmStatic
3+
fun main(args: Array<String>) {
4+
println(fibonacci(15))
5+
}
6+
7+
fun fibonacci(n: Int): Int {
8+
if (n == 0) return 0 // base case
9+
val f = IntArray(2)
10+
f[0] = 1
11+
f[1] = 1
12+
for (i in 2..n) {
13+
f[i % 2] = f[0] + f[1]
14+
}
15+
16+
return f[(n + 1) % 2]
17+
}
18+
}

0 commit comments

Comments
 (0)
Please sign in to comment.