- Notifications
You must be signed in to change notification settings - Fork 1.6k
/
Copy pathCatalanSequence.cs
37 lines (35 loc) · 926 Bytes
/
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
usingSystem.Collections.Generic;
usingSystem.Numerics;
namespaceAlgorithms.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>
publicclassCatalanSequence:ISequence
{
/// <summary>
/// Gets sequence of Catalan numbers.
/// </summary>
publicIEnumerable<BigInteger>Sequence
{
get
{
// initialize the first element (1) and define it's enumerator (0)
varcatalan=newBigInteger(1);
varn=0;
while(true)
{
yieldreturncatalan;
catalan=(2*(2*n+1)*catalan)/(n+2);
n++;
}
}
}
}