@@ -16,15 +16,17 @@ public static class CatalanNumbers
16
16
/// Internal cache.
17
17
/// By default contains the first two catalan numbers for the ranks: 0, and 1.
18
18
/// </summary>
19
- private static readonly Dictionary < uint , BigInteger > _catalanNumbers = new Dictionary < uint , BigInteger > { { 0 , 1 } , { 1 , 1 } } ;
19
+ private static readonly Dictionary < uint , BigInteger > CachedCatalanNumbers = new Dictionary < uint , BigInteger > { { 0 , 1 } , { 1 , 1 } } ;
20
20
21
21
/// <summary>
22
- /// Helper function .
22
+ /// Helper method .
23
23
/// </summary>
24
+ /// <param name="rank"></param>
25
+ /// <returns></returns>
24
26
private static BigInteger _recursiveHelper ( uint rank )
25
27
{
26
- if ( _catalanNumbers . ContainsKey ( rank ) )
27
- return _catalanNumbers [ rank ] ;
28
+ if ( CachedCatalanNumbers . ContainsKey ( rank ) )
29
+ return CachedCatalanNumbers [ rank ] ;
28
30
29
31
BigInteger number = 0 ;
30
32
var lastRank = rank - 1 ;
@@ -34,8 +36,8 @@ private static BigInteger _recursiveHelper(uint rank)
34
36
var firstPart = _recursiveHelper ( i ) ;
35
37
var secondPart = _recursiveHelper ( lastRank - i ) ;
36
38
37
- if ( ! _catalanNumbers . ContainsKey ( i ) ) _catalanNumbers . Add ( i , firstPart ) ;
38
- if ( ! _catalanNumbers . ContainsKey ( lastRank - i ) ) _catalanNumbers . Add ( lastRank - i , secondPart ) ;
39
+ if ( ! CachedCatalanNumbers . ContainsKey ( i ) ) CachedCatalanNumbers . Add ( i , firstPart ) ;
40
+ if ( ! CachedCatalanNumbers . ContainsKey ( lastRank - i ) ) CachedCatalanNumbers . Add ( lastRank - i , secondPart ) ;
39
41
40
42
number = number + ( firstPart * secondPart ) ;
41
43
}
@@ -44,28 +46,35 @@ private static BigInteger _recursiveHelper(uint rank)
44
46
}
45
47
46
48
/// <summary>
47
- /// Public API
49
+ /// Public API.
48
50
/// </summary>
51
+ /// <param name="rank"></param>
52
+ /// <returns></returns>
49
53
public static BigInteger GetNumber ( uint rank )
50
54
{
51
55
// Assert the cache is not empty.
52
- Debug . Assert ( _catalanNumbers . Count >= 2 ) ;
56
+ Debug . Assert ( CachedCatalanNumbers . Count >= 2 ) ;
53
57
54
58
return _recursiveHelper ( rank ) ;
55
59
}
56
60
57
61
/// <summary>
58
62
/// Calculate the number using the Binomial Coefficients algorithm
59
63
/// </summary>
64
+ /// <param name="rank"></param>
65
+ /// <returns></returns>
60
66
public static BigInteger GetNumberByBinomialCoefficients ( uint rank )
61
67
{
62
68
// Calculate by binomial coefficient.
63
69
return BinomialCoefficients . Calculate ( rank ) ;
64
70
}
65
71
66
72
/// <summary>
67
- /// Return the list of catalan numbers between two ranks, inclusive.
73
+ /// Return the list of catalan numbers between two ranks, inclusive
68
74
/// </summary>
75
+ /// <param name="fromRank"></param>
76
+ /// <param name="toRank"></param>
77
+ /// <returns></returns>
69
78
public static List < BigInteger > GetRange ( uint fromRank , uint toRank )
70
79
{
71
80
var numbers = new List < BigInteger > ( ) ;
@@ -79,4 +88,4 @@ public static List<BigInteger> GetRange(uint fromRank, uint toRank)
79
88
return numbers ;
80
89
}
81
90
}
82
- }
91
+ }
0 commit comments