Added CSharp Fibonacci Checker program (#913)

pull/958/head
Katherine Hambley 2022-10-06 08:01:48 -05:00 committed by GitHub
parent 6b7530c587
commit 75e93fd885
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 53 additions and 0 deletions

View File

@ -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

View File

@ -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;
}
}
}

View File

@ -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);
}
}
}