Last active
May 23, 2019 01:55
-
-
Save DiegoGSantos/db207771c2f4400cfad75e1894954ac9 to your computer and use it in GitHub Desktop.
A function to get next fibonacci series
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
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