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
  • Practice
  • Certification
  • Compete
  • Career Fair
  • Hiring developers?
  1. Practice
  2. Functional Programming
  3. Recursion
  4. Fibonacci Numbers

Fibonacci Numbers

Problem
Submissions
Leaderboard
Discussions

Objective
In this challenge, we learn about using the Fibonacci Function.

Resources
Here's a helpful video on the topic:

The Fibonacci Series

The Fibonacci sequence begins with and . These are the first and second terms, respectively. After this, every element is the sum of the preceding elements:

Fibonacci(n) = Fibonacci(n-1) + Fibonacci(n-2)  

Task
Given the starter code, complete the Fibonacci function to return the term.

We start counting from Fibonacci. This might differ from some other notations that treats Fibonacci.

The overall equation is:

             = 0 , n = 1
Fibonacci(n) = 1 , n = 2
               Fibonacci(n-1) + Fibonacci(n-2)  , n > 2

Input Format

One line of input, the integer .

Constraints

Output Format

Output one integer, the Fibonacci number.

Sample Input

3  

Sample Output

1  

Function Prototype
The starter code is provided for Scala. The code for accepting the input and displaying the output is provided. You will be provided the input parameter , and you need to return the Fibonacci term.

Sample Input and Output Values for the Fibonacci Series

fibonacci(3) = (0+1) = 1  
fibonacci(4) = (1+1) = 2  
fibonacci(5) = (1+2) = 3  

Requirements
Simple test cases can be cleared with a purely recursive function exponentially. To clear the more challenging test cases without violating the principles of functional programming, you might benefit from learning about the accumulator technique.

Author

PRASHANTB1984

Difficulty

Easy

Max Score

2

Submitted By

11733

Need Help?


View discussions
View top submissions

rate this challenge

MORE DETAILS

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