Skip to content

Instantly share code, notes, and snippets.

@DiegoGSantos
Last active May 23, 2019 01:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DiegoGSantos/db207771c2f4400cfad75e1894954ac9 to your computer and use it in GitHub Desktop.
Save DiegoGSantos/db207771c2f4400cfad75e1894954ac9 to your computer and use it in GitHub Desktop.
A function to get next fibonacci series
var hashMap = HashMap<Int, Long>()
private fun fibonacci(i: Long): Long {
if(hashMap.containsKey(i)) {
return hashMap[i] ?: 0
}
var result: Long = if (i <= 2) 1 else fibonacci(i - 1) + fibonacci(i - 2)
hashMap[i] = result
return result
}
private fun nextFibonacci(number: Long) {
var index = 0L
var fibonacciNumber = 0L
try {
while (fibonacciNumber <= number) {
fibonacciNumber = fibonacci(index)
index++
}
println(fibonacciNumber)
} catch (e: Exception) {
println(0)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment