Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- My:
- object Sol {
- def fib(n: Int): BigInt = {
- @annotation.tailrec
- def getTailRec(index: Int, prev: BigInt, current: BigInt): BigInt = {
- if (index <= 0) {
- current
- } else {
- getTailRec(index - 1, prev = prev + current, current = prev)
- }
- }
- getTailRec(n, prev = 1, current = 0)
- }
- }
- Other:
- import scala.collection.mutable.Map
- object Sol {
- val cached: Map[Int, BigInt] = Map(0 -> 0, 1 -> 1)
- def fib(n: Int): BigInt =
- cached.getOrElseUpdate(n, fib(n-1) + fib(n-2))
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement