Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Binomial, Factorial sequences added, existing updated with length parameter, minor refactors in AATreeTest, readme updated #199

Merged
merged 22 commits into from
Jan 23, 2021
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
f7e1e55
minor refactors in AATreeTests
kolosovpetro Jan 21, 2021
632bbe4
fibonacci sequence length is fixed
kolosovpetro Jan 21, 2021
b409e6f
natural sequence length is set in constructor, test updated
kolosovpetro Jan 21, 2021
5fb0a7d
prime sequence is generated up to bound value, set in constructor
kolosovpetro Jan 21, 2021
824e5bd
binomial sequence added
kolosovpetro Jan 21, 2021
5f08fee
factorial sequence added
kolosovpetro Jan 21, 2021
16b4378
readme updated
kolosovpetro Jan 21, 2021
4b378b2
factorial sequence updated according to comments
kolosovpetro Jan 21, 2021
3d1d72b
binomial sequence is updated according to comments
kolosovpetro Jan 21, 2021
ef4929e
fibonacci sequence updated
kolosovpetro Jan 21, 2021
d2461dd
prime and natural sequences updated
kolosovpetro Jan 21, 2021
d89c517
readme modified
kolosovpetro Jan 21, 2021
7e9b81e
AAtree constructor test revised
kolosovpetro Jan 21, 2021
75725fb
AAtree add multiple keys test revised
kolosovpetro Jan 21, 2021
c9c048e
AAtree addrange keys test revised
kolosovpetro Jan 21, 2021
fcb13b9
AAtree remove multiple keys test revised
kolosovpetro Jan 21, 2021
8aa3373
AAtree other tests are updated using Fluent Assertions
kolosovpetro Jan 21, 2021
b36269e
factorial sequence updated
kolosovpetro Jan 22, 2021
33c6c84
fibonacci sequence updated
kolosovpetro Jan 22, 2021
8a55a0f
prime sequence updated
kolosovpetro Jan 22, 2021
5e06d3b
natural sequence updated
kolosovpetro Jan 22, 2021
0fb8cf0
binomial sequence updated
kolosovpetro Jan 22, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions Algorithms.Tests/Sequences/BinomialSequenceTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System.Numerics;
using Algorithms.Sequences;
using NUnit.Framework;

namespace Algorithms.Tests.Sequences
{
public class BinomialSequenceTests
{
[Test]
public void First4RowsCorrect()
{
var sequence = new BinomialSequence(4).Sequence;
Assert.AreEqual(new BigInteger[] { 1, 1, 1, 1, 2, 1, 1, 3, 3, 1 }, sequence);
}
}
}
17 changes: 17 additions & 0 deletions Algorithms.Tests/Sequences/FactorialSequenceTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System.Numerics;
using Algorithms.Sequences;
using NUnit.Framework;

namespace Algorithms.Tests.Sequences
{
public class FactorialSequenceTest
{
[Test]
public void First10ItemsCorrect()
{
var sequence = new FactorialSequence(10).Sequence;
Assert.AreEqual(new BigInteger[] { 1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880 },
sequence);
}
}
}
7 changes: 3 additions & 4 deletions Algorithms.Tests/Sequences/FibonacciSequenceTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Linq;
using System.Numerics;
using System.Numerics;
using Algorithms.Sequences;
using NUnit.Framework;

Expand All @@ -10,9 +9,9 @@ public class FibonacciSequenceTests
[Test]
public void First10ElementsCorrect()
{
var sequence = new FibonacciSequence().Sequence;
var sequence = new FibonacciSequence(10).Sequence;

Assert.AreEqual(new BigInteger[] { 0, 1, 1, 2, 3, 5, 8, 13, 21, 34 }, sequence.Take(10));
Assert.AreEqual(new BigInteger[] { 0, 1, 1, 2, 3, 5, 8, 13, 21, 34 }, sequence);
}
}
}
7 changes: 3 additions & 4 deletions Algorithms.Tests/Sequences/NaturalSequenceTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Linq;
using System.Numerics;
using System.Numerics;
using Algorithms.Sequences;
using NUnit.Framework;

Expand All @@ -10,9 +9,9 @@ public class NaturalSequenceTests
[Test]
public void First10ElementsCorrect()
{
var sequence = new NaturalSequence().Sequence;
var sequence = new NaturalSequence(10).Sequence;

Assert.AreEqual(new BigInteger[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }, sequence.Take(10));
Assert.AreEqual(new BigInteger[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }, sequence);
}
}
}
7 changes: 3 additions & 4 deletions Algorithms.Tests/Sequences/PrimesSequenceTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Linq;
using System.Numerics;
using System.Numerics;
using Algorithms.Sequences;
using NUnit.Framework;

Expand All @@ -10,9 +9,9 @@ public class PrimesSequenceTests
[Test]
public void First10ElementsCorrect()
{
var sequence = new PrimesSequence().Sequence;
var sequence = new PrimesSequence(30).Sequence;

Assert.AreEqual(new BigInteger[] { 2, 3, 5, 7, 11, 13, 17, 19, 23, 29 }, sequence.Take(10));
Assert.AreEqual(new BigInteger[] { 2, 3, 5, 7, 11, 13, 17, 19, 23, 29 }, sequence);
}
}
}
66 changes: 66 additions & 0 deletions Algorithms/Sequences/BinomialSequence.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
using System.Collections.Generic;
using System.Numerics;

namespace Algorithms.Sequences
{
/// <summary>
/// <para>
/// Sequence of binomial coefficients.
/// </para>
/// <para>
/// Wikipedia: https://en.wikipedia.org/wiki/Binomial_coefficient.
/// </para>
/// <para>
/// OEIS: http://oeis.org/A007318.
/// </para>
/// </summary>
public class BinomialSequence : ISequence
{
private readonly long sequenceLength;

public BinomialSequence(long sequenceLength) => this.sequenceLength = sequenceLength;

/// <summary>
/// Gets sequence of binomial coefficients.
/// </summary>
public IEnumerable<BigInteger> Sequence => GenerateBinomialSequence(sequenceLength);

private static IEnumerable<BigInteger> GenerateBinomialSequence(long rowNumbers)
{
for (long i = 0; i < rowNumbers; i++)
{
var row = GenerateRow(i);
foreach (var coefficient in row)
{
yield return coefficient;
}
}
}

private static BigInteger BinomialCoefficient(long n, long k)
{
if (k == 0 || k == n)
{
return new BigInteger(1);
}

if (n < 0)
{
return new BigInteger(0);
}

return BinomialCoefficient(n - 1, k) + BinomialCoefficient(n - 1, k - 1);
}

private static IEnumerable<BigInteger> GenerateRow(long n)
{
long k = 0;

while (k <= n)
{
yield return BinomialCoefficient(n, k);
k++;
}
}
}
}
50 changes: 50 additions & 0 deletions Algorithms/Sequences/FactorialSequence.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
using System.Collections.Generic;
using System.Linq;
using System.Numerics;

namespace Algorithms.Sequences
{
/// <summary>
/// <para>
/// Sequence of factorial numbers.
/// </para>
/// <para>
/// Wikipedia: https://en.wikipedia.org/wiki/Factorial.
/// </para>
/// <para>
/// OEIS: https://oeis.org/A000142.
/// </para>
/// </summary>
public class FactorialSequence : ISequence
{
private readonly long sequenceLength;

public FactorialSequence(long sequenceLength) => this.sequenceLength = sequenceLength;

/// <summary>
/// Gets sequence of binomial coefficients.
/// </summary>
public IEnumerable<BigInteger> Sequence => GenerateFactorialSequence(sequenceLength);

private static IEnumerable<BigInteger> GenerateFactorialSequence(long size)
{
var k = 0;

while (k < size)
{
yield return Factorial(k);
k++;
}
}

private static BigInteger Factorial(long n)
{
if (n == 0)
{
return 1;
}

return n * Factorial(n - 1);
}
}
}
27 changes: 14 additions & 13 deletions Algorithms/Sequences/FibonacciSequence.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,26 @@ namespace Algorithms.Sequences
/// </summary>
public class FibonacciSequence : ISequence
{
private readonly long sequenceLength;

public FibonacciSequence(long sequenceLength) => this.sequenceLength = sequenceLength;

/// <summary>
/// Gets Fibonacci sequence.
/// </summary>
public IEnumerable<BigInteger> Sequence
public IEnumerable<BigInteger> Sequence => GenerateFibonacciSequence(sequenceLength);

private static IEnumerable<BigInteger> GenerateFibonacciSequence(long size)
{
get
var seq = new BigInteger[size];
seq[1] = 1;

for (var i = 2; i < size; i++)
{
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;
}
seq[i] = seq[i - 1] + seq[i - 2];
}

return seq;
}
}
}
19 changes: 12 additions & 7 deletions Algorithms/Sequences/NaturalSequence.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,23 @@ namespace Algorithms.Sequences
/// </summary>
public class NaturalSequence : ISequence
{
private readonly long sequenceLength;

public NaturalSequence(long sequenceLength) => this.sequenceLength = sequenceLength;

/// <summary>
/// Gets sequence of natural numbers.
/// </summary>
public IEnumerable<BigInteger> Sequence
public IEnumerable<BigInteger> Sequence => GenerateSequence(sequenceLength);

private static IEnumerable<BigInteger> GenerateSequence(long size)
{
get
var current = new BigInteger(1);

while (current <= size)
{
var n = new BigInteger(1);
while (true)
{
yield return n++;
}
yield return current;
current++;
}
}
}
Expand Down
31 changes: 15 additions & 16 deletions Algorithms/Sequences/PrimesSequence.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,31 +17,30 @@ namespace Algorithms.Sequences
/// </summary>
public class PrimesSequence : ISequence
{
private readonly long sequenceLength;

public PrimesSequence(long sequenceLength) => this.sequenceLength = sequenceLength;

/// <summary>
/// Gets sequence of prime numbers.
/// </summary>
public IEnumerable<BigInteger> Sequence
public IEnumerable<BigInteger> Sequence => GeneratePrimeSequence(sequenceLength);

private static IEnumerable<BigInteger> GeneratePrimeSequence(long limitValue)
{
get
var list = new List<BigInteger> { 2 };

for (var i = 3; i < limitValue; i++)
{
yield return 2;
var primes = new List<BigInteger>
{
2,
};
var n = new BigInteger(3);
var current = new BigInteger(i);

while (true)
if (list.All(x => current % x != 0))
{
if (primes.All(p => n % p != 0))
{
yield return n;
primes.Add(n);
}

n += 2;
list.Add(current);
}
}

return list;
}
}
}
Loading