From 75e93fd88521cc57723ab0d62550db3f7ddafde7 Mon Sep 17 00:00:00 2001 From: Katherine Hambley Date: Thu, 6 Oct 2022 08:01:48 -0500 Subject: [PATCH] Added CSharp Fibonacci Checker program (#913) --- algorithms/CSharp/README.md | 1 + .../CSharp/src/Maths/fibonacci-checker.cs | 32 +++++++++++++++++++ .../test/Maths/fibonacci-checker-tests.cs | 20 ++++++++++++ 3 files changed, 53 insertions(+) create mode 100644 algorithms/CSharp/src/Maths/fibonacci-checker.cs create mode 100644 algorithms/CSharp/test/Maths/fibonacci-checker-tests.cs diff --git a/algorithms/CSharp/README.md b/algorithms/CSharp/README.md index 5f17d0b7..edee912a 100644 --- a/algorithms/CSharp/README.md +++ b/algorithms/CSharp/README.md @@ -40,6 +40,7 @@ To run the `.cs` file, kindly use [.Net Finddle](https://dotnetfiddle.net/) ## Maths - [Abundant Number](src/Maths/abundant-number.cs) +- [Fibonacci Checker](src/Maths/fibonacci-checker.cs) - [Naismith's Rule](src/Maths/naismith-rule.cs) ## Queues diff --git a/algorithms/CSharp/src/Maths/fibonacci-checker.cs b/algorithms/CSharp/src/Maths/fibonacci-checker.cs new file mode 100644 index 00000000..61cc94a8 --- /dev/null +++ b/algorithms/CSharp/src/Maths/fibonacci-checker.cs @@ -0,0 +1,32 @@ +/* Fibonacci Checker - A program to check whether a number is in the Fibonacci sequence. +This sequence is defined by: Fn = Fn-1 + Fn-2 (to find Fn, you add the previous two numbers. The program calculates the Fibonacci sequence up to and including the given number. */ +using System; + +namespace Algorithms.Maths +{ + public class Fibonacci + { + public static bool Checker(int num) + { + if (num == 2 || num == 3) + { + return true; + } + var num1 = 0; + var num2 = 1; + var num3 = 1; + + for (var i = 0; i <= num; i++) + { + if (num1 == num) + { + return true; + } + num1 = num2; + num2 = num3; + num3 = num1 + num2; + } + return false; + } + } +} diff --git a/algorithms/CSharp/test/Maths/fibonacci-checker-tests.cs b/algorithms/CSharp/test/Maths/fibonacci-checker-tests.cs new file mode 100644 index 00000000..25a2c58b --- /dev/null +++ b/algorithms/CSharp/test/Maths/fibonacci-checker-tests.cs @@ -0,0 +1,20 @@ +using NUnit.Framework; + +namespace Algorithms.Tests.Maths +{ + [TestFixture] + internal class Checker + { + [TestCase(1, true)] + [TestCase(3, true)] + [TestCase(4, false)] + [TestCase(34, true)] + [TestCase(70, false)] + [TestCase(110, false)] + public void FibonacciChecker_ShouldGetExpectedResult(int num, bool expected) + { + var result = Algorithms.Maths.Fibonacci.Checker(num); + Assert.AreEqual(expected, result); + } + } +}