Skip to content

Commit 2f24696

Browse files
authored
Add Binomial and Factorial sequences (#199)
1 parent 2c63986 commit 2f24696

9 files changed

Lines changed: 205 additions & 91 deletions

File tree

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using System.Linq;
2+
using System.Numerics;
3+
using Algorithms.Sequences;
4+
using FluentAssertions;
5+
using NUnit.Framework;
6+
7+
namespace Algorithms.Tests.Sequences
8+
{
9+
public class BinomialSequenceTests
10+
{
11+
[Test]
12+
public void First4RowsCorrect()
13+
{
14+
var sequence = new BinomialSequence().Sequence.Take(10);
15+
sequence.SequenceEqual(new BigInteger[] {1, 1, 1, 1, 2, 1, 1, 3, 3, 1})
16+
.Should().BeTrue();
17+
}
18+
}
19+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using System.Linq;
2+
using System.Numerics;
3+
using Algorithms.Sequences;
4+
using FluentAssertions;
5+
using NUnit.Framework;
6+
7+
namespace Algorithms.Tests.Sequences
8+
{
9+
public class FactorialSequenceTest
10+
{
11+
[Test]
12+
public void First10ItemsCorrect()
13+
{
14+
var sequence = new FactorialSequence().Sequence.Take(10);
15+
sequence.SequenceEqual(new BigInteger[] {1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880})
16+
.Should().BeTrue();
17+
}
18+
}
19+
}
Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System.Linq;
22
using System.Numerics;
33
using Algorithms.Sequences;
4+
using FluentAssertions;
45
using NUnit.Framework;
56

67
namespace Algorithms.Tests.Sequences
@@ -10,9 +11,9 @@ public class FibonacciSequenceTests
1011
[Test]
1112
public void First10ElementsCorrect()
1213
{
13-
var sequence = new FibonacciSequence().Sequence;
14-
15-
Assert.AreEqual(new BigInteger[] { 0, 1, 1, 2, 3, 5, 8, 13, 21, 34 }, sequence.Take(10));
14+
var sequence = new FibonacciSequence().Sequence.Take(10);
15+
sequence.SequenceEqual(new BigInteger[] {0, 1, 1, 2, 3, 5, 8, 13, 21, 34})
16+
.Should().BeTrue();
1617
}
1718
}
1819
}
Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System.Linq;
22
using System.Numerics;
33
using Algorithms.Sequences;
4+
using FluentAssertions;
45
using NUnit.Framework;
56

67
namespace Algorithms.Tests.Sequences
@@ -10,9 +11,9 @@ public class NaturalSequenceTests
1011
[Test]
1112
public void First10ElementsCorrect()
1213
{
13-
var sequence = new NaturalSequence().Sequence;
14-
15-
Assert.AreEqual(new BigInteger[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }, sequence.Take(10));
14+
var sequence = new NaturalSequence().Sequence.Take(10);
15+
sequence.SequenceEqual(new BigInteger[] {1, 2, 3, 4, 5, 6, 7, 8, 9, 10})
16+
.Should().BeTrue();
1617
}
1718
}
1819
}
Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System.Linq;
22
using System.Numerics;
33
using Algorithms.Sequences;
4+
using FluentAssertions;
45
using NUnit.Framework;
56

67
namespace Algorithms.Tests.Sequences
@@ -10,9 +11,9 @@ public class PrimesSequenceTests
1011
[Test]
1112
public void First10ElementsCorrect()
1213
{
13-
var sequence = new PrimesSequence().Sequence;
14-
15-
Assert.AreEqual(new BigInteger[] { 2, 3, 5, 7, 11, 13, 17, 19, 23, 29 }, sequence.Take(10));
14+
var sequence = new PrimesSequence().Sequence.Take(10);
15+
sequence.SequenceEqual(new BigInteger[] {2, 3, 5, 7, 11, 13, 17, 19, 23, 29})
16+
.Should().BeTrue();
1617
}
1718
}
1819
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
using System.Collections.Generic;
2+
using System.Numerics;
3+
4+
namespace Algorithms.Sequences
5+
{
6+
/// <summary>
7+
/// <para>
8+
/// Sequence of binomial coefficients.
9+
/// </para>
10+
/// <para>
11+
/// Wikipedia: https://en.wikipedia.org/wiki/Binomial_coefficient.
12+
/// </para>
13+
/// <para>
14+
/// OEIS: http://oeis.org/A007318.
15+
/// </para>
16+
/// </summary>
17+
public class BinomialSequence : ISequence
18+
{
19+
/// <summary>
20+
/// Gets sequence of binomial coefficients.
21+
/// </summary>
22+
public IEnumerable<BigInteger> Sequence
23+
{
24+
get
25+
{
26+
var i = 0;
27+
28+
while (true)
29+
{
30+
var row = GenerateRow(i);
31+
foreach (var coefficient in row)
32+
{
33+
yield return coefficient;
34+
}
35+
36+
i++;
37+
}
38+
}
39+
}
40+
41+
private static BigInteger BinomialCoefficient(long n, long k)
42+
{
43+
if (k == 0 || k == n)
44+
{
45+
return new BigInteger(1);
46+
}
47+
48+
if (n < 0)
49+
{
50+
return new BigInteger(0);
51+
}
52+
53+
return BinomialCoefficient(n - 1, k) + BinomialCoefficient(n - 1, k - 1);
54+
}
55+
56+
private static IEnumerable<BigInteger> GenerateRow(long n)
57+
{
58+
long k = 0;
59+
60+
while (k <= n)
61+
{
62+
yield return BinomialCoefficient(n, k);
63+
k++;
64+
}
65+
}
66+
}
67+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
using System.Collections.Generic;
2+
using System.Numerics;
3+
4+
namespace Algorithms.Sequences
5+
{
6+
/// <summary>
7+
/// <para>
8+
/// Sequence of factorial numbers.
9+
/// </para>
10+
/// <para>
11+
/// Wikipedia: https://en.wikipedia.org/wiki/Factorial.
12+
/// </para>
13+
/// <para>
14+
/// OEIS: https://oeis.org/A000142.
15+
/// </para>
16+
/// </summary>
17+
public class FactorialSequence : ISequence
18+
{
19+
/// <summary>
20+
/// Gets sequence of factorial numbers.
21+
/// </summary>
22+
public IEnumerable<BigInteger> Sequence
23+
{
24+
get
25+
{
26+
var n = 0;
27+
var factorial = new BigInteger(1);
28+
while (true)
29+
{
30+
yield return factorial;
31+
n++;
32+
factorial *= n;
33+
}
34+
}
35+
}
36+
}
37+
}

0 commit comments

Comments
 (0)