How to implement Fibonacci using topDown approach using C#?

The Fibonacci sequence is a set of numbers that starts with a one or a zero, followed by a one, and proceeds based on the rule that each number (called a Fibonacci number) is equal to the sum of the preceding two numbers. The top-down approach focuses on breaking down a big problem into smaller and understandable chunks using memoization to store previously computed values.

This approach uses recursion with an additional array to cache results, avoiding redundant calculations and significantly improving performance over naive recursion.

Complexity Analysis

Time complexity − O(N) because each Fibonacci number is calculated only once.

Space complexity − O(N) because we create an extra array for memoization plus O(N) recursion stack space.

Syntax

Following is the basic structure for implementing Fibonacci using top-down approach −

public int Fibonacci(int n, int[] memo) {
    if (base_case) return base_value;
    if (memo[n] != 0) return memo[n];
    memo[n] = Fibonacci(n-1, memo) + Fibonacci(n-2, memo);
    return memo[n];
}

How It Works

The top-down approach works by −

  • Recursion: Breaking the problem into smaller subproblems (F(n) = F(n-1) + F(n-2))

  • Memoization: Storing results in an array to avoid recalculating the same values

  • Base cases: F(0) = 0 and F(1) = 1

Example

using System;

public class DynamicProgramming {
    public int FibonacciTopdownApproach(int n, int[] dpArr) {
        if (n == 0 || n == 1) {
            return n;
        }
        
        if (dpArr[n] != 0) {
            return dpArr[n];
        }
        
        int res = FibonacciTopdownApproach(n - 1, dpArr) + FibonacciTopdownApproach(n - 2, dpArr);
        return dpArr[n] = res;
    }
    
    public static void Main(string[] args) {
        DynamicProgramming dp = new DynamicProgramming();
        int[] dpArr = new int[13];
        
        Console.WriteLine("Fibonacci of 12: " + dp.FibonacciTopdownApproach(12, dpArr));
        
        // Display the sequence
        Console.Write("Fibonacci sequence: ");
        for (int i = 0; i 

The output of the above code is −

Fibonacci of 12: 144
Fibonacci sequence: 0 1 1 2 3 5 8 13 21 34 55 89 144 

Comparison with Other Approaches

Approach Time Complexity Space Complexity Method
Naive Recursion O(2^n) O(n) Pure recursion
Top-Down (Memoization) O(n) O(n) Recursion + memoization
Bottom-Up (Tabulation) O(n) O(n) Iterative approach

Optimized Version

Here's a more optimized version that initializes the memoization array internally −

using System;

public class OptimizedFibonacci {
    public int Fibonacci(int n) {
        int[] memo = new int[n + 1];
        return FibonacciHelper(n, memo);
    }
    
    private int FibonacciHelper(int n, int[] memo) {
        if (n <= 1) return n;
        
        if (memo[n] != 0) return memo[n];
        
        memo[n] = FibonacciHelper(n - 1, memo) + FibonacciHelper(n - 2, memo);
        return memo[n];
    }
    
    public static void Main(string[] args) {
        OptimizedFibonacci fib = new OptimizedFibonacci();
        
        Console.WriteLine("F(10) = " + fib.Fibonacci(10));
        Console.WriteLine("F(20) = " + fib.Fibonacci(20));
        Console.WriteLine("F(30) = " + fib.Fibonacci(30));
    }
}

The output of the above code is −

F(10) = 55
F(20) = 6765
F(30) = 832040

Conclusion

The top-down approach for Fibonacci using memoization provides an optimal balance between readability and performance with O(n) time complexity. It transforms the exponential time complexity of naive recursion into linear time by storing intermediate results, making it suitable for computing large Fibonacci numbers efficiently.

Updated on: 2026-03-17T07:04:36+05:30

922 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements