The nth Fibonacci Number
InterviewTime limit1sMemory limit256 MB
Given n up to 20, compute the nth Fibonacci number defined from 0 and 1.
- Level
Easy1 of 10
- Topics
- Dynamic programming, Recursion
- Solved
- No attempts yet
Problem
The Fibonacci sequence starts with 0 and 1. The 0th Fibonacci number is 0, and the 1st is 1. From index 2 on, each number is the sum of the two numbers before it.
As a formula, for .
Listed up to , the sequence is:
Given , write a program that computes the th Fibonacci number.
Input
The first line contains . It is either 0 or a natural number less than or equal to 20.
Output
Print the th Fibonacci number on the first line.