You are viewing a single comment's thread. Return to all comments →
A fast way to calculate the Fibonacci numbers is to generate them in one go.
import scala.io.StdIn.readInt object Solution { var M = 100000007 def fib(n: Int): List[Long] = { (2 to n).foldLeft(List[Long](1, 0)){ case(h1 :: h2 :: tail, n) => (h1 + h2) % M :: h1 :: h2 :: tail } } def main(args: Array[String]): Unit = { val cases = (1 to readInt).map(_ => readInt) val fibs = fib(cases.max).reverse cases.foreach(i => println(fibs(i))) } }
Seems like cookies are disabled on this browser, please enable them to open this website
Fibonacci
You are viewing a single comment's thread. Return to all comments →
A fast way to calculate the Fibonacci numbers is to generate them in one go.