forked from TheAlgorithms/C-Sharp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CatalanSequence.cs
38 lines (37 loc) · 1.03 KB
/
CatalanSequence.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
using System.Collections.Generic;
using System.Numerics;
namespace Algorithms.Sequences
{
/// <summary>
/// <para>
/// Catalan numbers: C[n+1] = (2*(2*n+1)*C[n])/(n+2).
/// </para>
/// <para>
/// Wikipedia: https://en.wikipedia.org/wiki/Catalan_number.
/// </para>
/// <para>
/// OEIS:http://oeis.org/A000108.
/// </para>
/// </summary>
public class CatalanSequence : ISequence
{
/// <summary>
/// Gets sequence of Catalan numbers.
/// </summary>
public IEnumerable<BigInteger> Sequence
{
get
{
// initialize the first element (1) and define it's enumerator (0)
var catalan = new BigInteger(1);
var n = 0;
while (true)
{
yield return catalan;
catalan = (2 * (2 * n + 1) * catalan) / (n + 2);
n++;
}
}
}
}
}