We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
  • HackerRank Home

    HackerRank

  • |
  • Prepare
  • Certify
  • Compete
  • Hiring developers?
  1. Recursion: Fibonacci Numbers

Recursion: Fibonacci Numbers

Problem
Submissions
Leaderboard
Discussions
Editorial
Topics

The Fibonacci Sequence

The Fibonacci sequence appears in nature all around us, in the arrangement of seeds in a sunflower and the spiral of a nautilus for example.

The Fibonacci sequence begins with and as its first and second terms. After these first two elements, each subsequent element is equal to the sum of the previous two elements.

Programmatically:

Given , return the number in the sequence.

Example

The Fibonacci sequence to is . With zero-based indexing, .

Function Description

Complete the recursive function in the editor below.

fibonacci has the following parameter(s):

  • int n: the index of the sequence to return

Returns
- int: the element in the Fibonacci sequence

Input Format

The integer .

Constraints

Sample Input

STDIN   Function
-----   --------
3       n = 3

Sample Output

2

Explanation

The Fibonacci sequence begins as follows:








...

In the sequence above, is .

Author

PRASHANTB1984

Difficulty

Easy

Max Score

15

Submitted By

127216

Need Help?


View discussions
View editorial
View top submissions
RESOURCES

  • Recursion

rate this challenge

MORE DETAILS

Download problem statement
Download sample test cases
Suggest Edits
  • Blog
  • Scoring
  • Environment
  • FAQ
  • About Us
  • Support
  • Careers
  • Terms Of Service
  • Privacy Policy