Added CSharp Fibonacci Checker program (#913)
parent
6b7530c587
commit
75e93fd885
|
@ -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
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue