Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Jun 30, 2025

This PR implements multiple approaches to calculating Fibonacci numbers, each demonstrating different algorithmic strategies and performance characteristics.

Implementations Added

1. Recursive - O(2^n) time complexity

// Simple recursive implementation following mathematical definition
public long Calculate(int n) {
    if (n == 0) return 0;
    if (n == 1) return 1;
    return Calculate(n - 1) + Calculate(n - 2);
}

2. Iterative - O(n) time, O(1) space

// Most efficient for general use
long prev2 = 0, prev1 = 1, current = 0;
for (int i = 2; i <= n; i++) {
    current = prev1 + prev2;
    prev2 = prev1;
    prev1 = current;
}

3. Memoized Recursive - O(n) time and space

  • Recursive with caching to avoid redundant calculations
  • Good for multiple Fibonacci number calculations

4. Dynamic Programming - O(n) time and space

  • Bottom-up approach building a table of results
  • Useful when you need all Fibonacci numbers up to n

5. Matrix Exponentiation - O(log n) time

  • Uses matrix multiplication and fast exponentiation
  • Most efficient for very large n values

Features

  • Comprehensive Test Suite: 17 unit tests covering correctness, consistency, edge cases, and performance validation
  • Performance Benchmark: Demonstrates clear performance differences between implementations
  • Documentation: Detailed README explaining complexity analysis and use cases
  • Clean Architecture: Interface-based design following .NET best practices

Performance Results

The benchmark shows dramatic performance differences:

Calculating Fibonacci(35):
Implementation           Result         Time (ms)
--------------------------------------------------
Iterative                9227465        0.00
Matrix Exponentiation    9227465        0.00  
Dynamic Programming      9227465        0.00
Memoized Recursive       9227465        0.00
Recursive                9227465        147.00

Project Structure

Fibonacci/Multiple-Implementations/
├── FibonacciImplementations/     # Class library with all implementations
├── FibonacciTests/              # Comprehensive unit tests  
├── FibonacciBenchmark/          # Performance comparison tool
├── FibonacciImplementations.sln # Solution file
└── README.md                    # Detailed documentation

This demonstrates how algorithmic choices dramatically impact performance, from exponential time complexity in the naive recursive approach to logarithmic time in the matrix exponentiation method.

Fixes #1.


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot AI and others added 2 commits June 30, 2025 16:17
Co-authored-by: svoff <35914639+svoff@users.noreply.github.com>
Co-authored-by: svoff <35914639+svoff@users.noreply.github.com>
Copilot AI changed the title [WIP] Create multiple implementations of the Fib function Create multiple implementations of the Fibonacci function with tests and benchmarks Jun 30, 2025
Copilot AI requested a review from svoff June 30, 2025 16:22
Copilot finished work on behalf of svoff June 30, 2025 16:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Create multiple implementations of the Fib function

2 participants