This Java program computes the Fibonacci sequence using dynamic programming. The Fibonacci sequence is defined by the recurrence relation:

F(n) = F(n-1) + F(n-2) with base cases F(0) = 0 and F(1) = 1.

Java Program


public class FibonacciDP {

// Function to compute the Fibonacci sequence using dynamic programming
public static int fibonacci(int n) {
if (n == 0) {
return 0;
} else if (n == 1) {
return 1;
}

// DP array to store Fibonacci numbers up to F(n)
int[] dp = new int[n + 1];

// Base cases
dp[0] = 0;
dp[1] = 1;

// Build the Fibonacci sequence from the bottom up
for (int i = 2; i <= n; i++) {
dp[i] = dp[i – 1] + dp[i – 2];
}

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

public static void main(String[] args) {
int n = 10; // Example: Find the 10th Fibonacci number

// Compute and print the Fibonacci number
int result = fibonacci(n);
System.out.println(“The ” + n + “th Fibonacci number is: ” + result);
}
}

Explanation of the Program Structure

This program computes the Fibonacci sequence using dynamic programming. Dynamic programming allows us to store the results of subproblems, avoiding the exponential time complexity of the naive recursive solution.

  • fibonacci function: This function computes the nth Fibonacci number using dynamic programming. It takes an integer n as input and returns F(n). The function uses a DP array to store the Fibonacci numbers calculated so far, allowing the program to reuse previously computed values.
  • Dynamic Programming Array (dp): The DP array dp[i] stores the ith Fibonacci number. The array is built from the bottom up, starting with the base cases dp[0] = 0 and dp[1] = 1. For each i from 2 to n, the Fibonacci number is calculated as dp[i] = dp[i-1] + dp[i-2].
  • Main logic: The program initializes the base cases for F(0) and F(1) and then computes each Fibonacci number up to F(n) using the recursive relation. Once the array is fully populated, the value at dp[n] is returned, which is the nth Fibonacci number.
  • Base cases: The base cases are handled separately. If n is 0 or 1, the function directly returns F(0) or F(1).
  • main function: This function initializes a sample value of n (in this case, 10) and calls the fibonacci function to compute the 10th Fibonacci number. The result is then printed to the console.

Step-by-Step Process:

  1. We initialize a DP array of size n + 1 to store the Fibonacci numbers from 0 to n.
  2. We set the base cases: dp[0] = 0 and dp[1] = 1.
  3. We iterate from i = 2 to n and compute each Fibonacci number using the relation dp[i] = dp[i-1] + dp[i-2].
  4. The final result is stored in dp[n], which is the nth Fibonacci number.

Example:

Consider finding the 10th Fibonacci number. The program will calculate the following sequence:


F(0) = 0, F(1) = 1, F(2) = 1, F(3) = 2, F(4) = 3, F(5) = 5,
F(6) = 8, F(7) = 13, F(8) = 21, F(9) = 34, F(10) = 55

The program will output:


The 10th Fibonacci number is: 55

Conclusion:

This Java program efficiently computes the Fibonacci sequence using dynamic programming. The time complexity of this solution is O(n), where n is the input number. This approach is far more efficient than the naive recursive solution, which has exponential time complexity. By storing the results of subproblems, we avoid redundant calculations and significantly improve performance.

 

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 :)