added fibonnaci in CSharp

pull/821/head
fonCki 2022-08-27 12:18:55 +02:00
parent 15ad9f3bd2
commit c23d3e07d6
3 changed files with 22 additions and 24 deletions

View File

@ -1,15 +1,16 @@
using System;
using System.Collections.Generic;
namespace Algorithms.Maths {
public class fibonacci_series {
// Fibonacci Series using Recursion and Memoization
// Fibonacci Series using Recursion and caching
public class FibonacciSequence {
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;
}
@ -17,11 +18,9 @@ namespace Algorithms.Maths {
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,18 @@
using System.Collections.Generic;
using NUnit.Framework;
namespace Algorithms.Tests.Maths {
[TestFixture]
public class fibonacci_seires {
[TestCase(20, new int[] { 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181 })]
[TestCase(30, new int[] { 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765, 10946, 17711, 28657, 46368, 75025, 121393, 196418, 317811, 514229 })]
public void CalculateFibonacciSeriesTest(int value, int[] expected) {
var result = Algorithms.Maths.FibonacciSequence.FibonacciSeries(value);
Assert.AreEqual(expected, result);
}
}
}

View File

@ -1,19 +0,0 @@
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);
}
}
}