Skip to content

Instantly share code, notes, and snippets.

@kmdsbng
Created March 7, 2018 13:23
Show Gist options
  • Save kmdsbng/034153cec0a918707bfc21f69a3db0e1 to your computer and use it in GitHub Desktop.
Save kmdsbng/034153cec0a918707bfc21f69a3db0e1 to your computer and use it in GitHub Desktop.
fun main(argv: Array<String>) {
println(fib(1))
println(fib(2))
println(fib(10))
println(fib(100))
}
fun fib(end: Int): Long {
if (end == 0) {
return 0L
}
if (end == 1) {
return 1L
}
return fibIter(2, end, 1L, 0L)
}
tailrec fun fibIter(cur: Int, end: Int, last: Long, last2: Long): Long {
if (end <= cur) {
return last + last2
}
return fibIter(cur + 1, end, last + last2, last)
}
// Warning: A function is marked as tail-recursive but no tail calls are found.
tailrec fun fibIterFailed(cur: Int, end: Int, last: Long, last2: Long): Long {
if (end <= cur) {
return last + last2
}
return fibIter(cur + 1, end, last + last2, last) + 1
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment