forked from TheAlgorithms/C-Sharp
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Separate sequences (TheAlgorithms#121)
* Move Fibonacci sequence to separate namespace * Update README.md
- Loading branch information
Showing
5 changed files
with
76 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
using System.Linq; | ||
using System.Numerics; | ||
using Algorithms.Sequences; | ||
using NUnit.Framework; | ||
|
||
namespace Algorithms.Tests.Sequences | ||
{ | ||
public class FibonacciSequenceTests | ||
{ | ||
[Test] | ||
public void First10ElementsCorrect() | ||
{ | ||
var sequence = new FibonacciSequence().Sequence; | ||
|
||
Assert.AreEqual(new BigInteger[] { 0, 1, 1, 2, 3, 5, 8, 13, 21, 34 }, sequence.Take(10)); | ||
} | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
using System.Collections.Generic; | ||
using System.Numerics; | ||
|
||
namespace Algorithms.Sequences | ||
{ | ||
/// <summary> | ||
/// <para> | ||
/// Fibonacci sequence. | ||
/// </para> | ||
/// <para> | ||
/// Wikipedia: https://wikipedia.org/wiki/Fibonacci_number. | ||
/// </para> | ||
/// <para> | ||
/// OEIS: https://oeis.org/A000045. | ||
/// </para> | ||
/// </summary> | ||
public class FibonacciSequence : ISequence | ||
{ | ||
/// <summary> | ||
/// Gets Fibonacci sequence. | ||
/// </summary> | ||
public IEnumerable<BigInteger> Sequence | ||
{ | ||
get | ||
{ | ||
yield return 0; | ||
yield return 1; | ||
BigInteger previous = 0; | ||
BigInteger current = 1; | ||
while (true) | ||
{ | ||
var next = previous + current; | ||
previous = current; | ||
current = next; | ||
yield return next; | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
using System.Collections.Generic; | ||
using System.Numerics; | ||
|
||
namespace Algorithms.Sequences | ||
{ | ||
/// <summary> | ||
/// Common interface for all integer sequences. | ||
/// </summary> | ||
public interface ISequence | ||
{ | ||
/// <summary> | ||
/// Gets sequence as enumerable. | ||
/// </summary> | ||
IEnumerable<BigInteger> Sequence { get; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters