The Fibonacci sequence is a series of numbers in which each number is the sum of the two preceding ones, usually starting with 0 and 1. The sequence typically goes: 0, 1, 1, 2, 3, 5, 8, 13, 21, etc. This document provides an efficient implementation of the Fibonacci sequence using dynamic programming.

Program Structure

  • Input: An integer n representing the position in the Fibonacci sequence.
  • Output: The nth Fibonacci number.
  • Dynamic Programming: The program uses an array to store previously computed Fibonacci numbers to avoid redundant calculations.

C++ Code


#include <iostream>
#include <vector>

using namespace std;

// Function to calculate the nth Fibonacci number using dynamic programming
int fibonacci(int n) {
    // Create an array to store Fibonacci numbers
    vector<int> fib(n + 1);

    // Base cases
    fib[0] = 0;
    if (n >= 1) {
        fib[1] = 1;
    }

    // Fill the array using the bottom-up approach
    for (int i = 2; i <= n; i++) {
        fib[i] = fib[i - 1] + fib[i - 2];
    }

    // Return the nth Fibonacci number
    return fib[n];
}

int main() {
    int n;

    // Input the position in the Fibonacci sequence
    cout << "Enter the position in the Fibonacci sequence: ";
    cin >> n;

    // Calculate the nth Fibonacci number
    int result = fibonacci(n);

    // Output the result
    cout << "The " << n << "th Fibonacci number is: " << result << endl;

    return 0;
}

Explanation of the Code

The code consists of the following main components:

  • Input Handling: The program prompts the user to enter the position n in the Fibonacci sequence.
  • Fibonacci Calculation: The fibonacci function uses dynamic programming to compute Fibonacci numbers. An array fib stores the Fibonacci numbers up to n.
  • Base Cases: The first two Fibonacci numbers (0 and 1) are initialized as base cases.
  • Dynamic Programming Logic: The function iterates from 2 to n to fill the array using the recurrence relation: fib[i] = fib[i - 1] + fib[i - 2].
  • Output: The program displays the nth Fibonacci number computed.

Conclusion

This program efficiently computes the Fibonacci sequence using dynamic programming techniques, demonstrating how to avoid redundant calculations through memoization. This approach significantly improves the time complexity compared to the naive recursive solution.

 

By Aditya Bhuyan

I work as a cloud specialist. In addition to being an architect and SRE specialist, I work as a cloud engineer and developer. I have assisted my clients in converting their antiquated programmes into contemporary microservices that operate on various cloud computing platforms such as AWS, GCP, Azure, or VMware Tanzu, as well as orchestration systems such as Docker Swarm or Kubernetes. For over twenty years, I have been employed in the IT sector as a Java developer, J2EE architect, scrum master, and instructor. I write about Cloud Native and Cloud often. Bangalore, India is where my family and I call home. I maintain my physical and mental fitness by doing a lot of yoga and meditation.

Leave a Reply

Your email address will not be published. Required fields are marked *

error

Enjoy this blog? Please spread the word :)