Rates TodayNIFTY25,670-0.83%SENSEX83,630.7-0.83%Gold 24K ₹11,320.24/g down186.46Silver ₹134.77/g down2.72
InsightforGeeks

Fibonacci number

MediumRecursion · JavaScript

The task

Write fib(n) that returns the nth Fibonacci number, where fib(0) = 0, fib(1) = 1 and every later number is the sum of the two before it.

Your solution must be fast: it will be tested with n up to 50, which a plain recursive version cannot finish in time.

Examples

  • fib(10) returns 55

Your code runs in your browser, in a separate worker, and is never sent to us. Edit the function, then press Run tests. Your work is saved here as you type.

JavaScript

Stuck?