From c23d3e07d69067b643c6075e25d7fb066c4ff409 Mon Sep 17 00:00:00 2001 From: fonCki Date: Sat, 27 Aug 2022 12:18:55 +0200 Subject: [PATCH] added fibonnaci in CSharp --- .../CSharp/src/Maths/fibonacci-series.cs | 9 ++++----- .../CSharp/test/Maths/fibonacci-seires.cs | 18 ++++++++++++++++++ .../test/Number-Theory/fibonacci-seires.cs | 19 ------------------- 3 files changed, 22 insertions(+), 24 deletions(-) create mode 100644 algorithms/CSharp/test/Maths/fibonacci-seires.cs delete mode 100644 algorithms/CSharp/test/Number-Theory/fibonacci-seires.cs diff --git a/algorithms/CSharp/src/Maths/fibonacci-series.cs b/algorithms/CSharp/src/Maths/fibonacci-series.cs index 1e287aee..5d0497e4 100644 --- a/algorithms/CSharp/src/Maths/fibonacci-series.cs +++ b/algorithms/CSharp/src/Maths/fibonacci-series.cs @@ -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(); 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]; } diff --git a/algorithms/CSharp/test/Maths/fibonacci-seires.cs b/algorithms/CSharp/test/Maths/fibonacci-seires.cs new file mode 100644 index 00000000..6daad2d0 --- /dev/null +++ b/algorithms/CSharp/test/Maths/fibonacci-seires.cs @@ -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); + } + + } +} \ No newline at end of file diff --git a/algorithms/CSharp/test/Number-Theory/fibonacci-seires.cs b/algorithms/CSharp/test/Number-Theory/fibonacci-seires.cs deleted file mode 100644 index cbb40e77..00000000 --- a/algorithms/CSharp/test/Number-Theory/fibonacci-seires.cs +++ /dev/null @@ -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); - } - - } -} \ No newline at end of file