fibonacci c# added

pull/821/head
fonCki 2022-08-27 07:45:04 +02:00
parent 8649eda4b1
commit 15ad9f3bd2
2 changed files with 49 additions and 0 deletions

View File

@ -0,0 +1,30 @@
using System.Collections.Generic;
namespace Algorithms.Maths {
public class fibonacci_series {
// Fibonacci Series using Recursion and Memoization
public static int[] FibonacciSeries(int n) {
var memo = new Dictionary<int, int>();
var result = new int[n];
for (int i = 0; i < n; i++) {
result[i] = Fibonacci(i, memo);
}
return result;
}
private static int Fibonacci(int n, Dictionary<int, int> memo) {
if (n == 0 || n == 1) {
return n;
}
if (memo.ContainsKey(n)) {
return memo[n];
}
memo[n] = Fibonacci(n - 1, memo) + Fibonacci(n - 2, memo);
return memo[n];
}
}
}

View File

@ -0,0 +1,19 @@
using System.Collections.Generic;
using NUnit.Framework;
namespace Algorithms.Tests.Number_Theory {
[TestFixture]
public class fibonacci_seires {
[TestCase(34, 0, 1, 1, 2, 3, 5, 8, 13, 21, 34)]
public void CalculateFibonacciSeriesTest(int value, int[] expected) {
var result = Algorithms.Maths.fibonacci_series.FibonacciSeries(value);
Assert.AreEqual(expected, result);
}
}
}