@@ -16,15 +16,17 @@ public static class CatalanNumbers
1616 /// Internal cache.
1717 /// By default contains the first two catalan numbers for the ranks: 0, and 1.
1818 /// </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 } } ;
2020
2121 /// <summary>
22- /// Helper function .
22+ /// Helper method .
2323 /// </summary>
24+ /// <param name="rank"></param>
25+ /// <returns></returns>
2426 private static BigInteger _recursiveHelper ( uint rank )
2527 {
26- if ( _catalanNumbers . ContainsKey ( rank ) )
27- return _catalanNumbers [ rank ] ;
28+ if ( CachedCatalanNumbers . ContainsKey ( rank ) )
29+ return CachedCatalanNumbers [ rank ] ;
2830
2931 BigInteger number = 0 ;
3032 var lastRank = rank - 1 ;
@@ -34,8 +36,8 @@ private static BigInteger _recursiveHelper(uint rank)
3436 var firstPart = _recursiveHelper ( i ) ;
3537 var secondPart = _recursiveHelper ( lastRank - i ) ;
3638
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 ) ;
3941
4042 number = number + ( firstPart * secondPart ) ;
4143 }
@@ -44,28 +46,35 @@ private static BigInteger _recursiveHelper(uint rank)
4446 }
4547
4648 /// <summary>
47- /// Public API
49+ /// Public API.
4850 /// </summary>
51+ /// <param name="rank"></param>
52+ /// <returns></returns>
4953 public static BigInteger GetNumber ( uint rank )
5054 {
5155 // Assert the cache is not empty.
52- Debug . Assert ( _catalanNumbers . Count >= 2 ) ;
56+ Debug . Assert ( CachedCatalanNumbers . Count >= 2 ) ;
5357
5458 return _recursiveHelper ( rank ) ;
5559 }
5660
5761 /// <summary>
5862 /// Calculate the number using the Binomial Coefficients algorithm
5963 /// </summary>
64+ /// <param name="rank"></param>
65+ /// <returns></returns>
6066 public static BigInteger GetNumberByBinomialCoefficients ( uint rank )
6167 {
6268 // Calculate by binomial coefficient.
6369 return BinomialCoefficients . Calculate ( rank ) ;
6470 }
6571
6672 /// <summary>
67- /// Return the list of catalan numbers between two ranks, inclusive.
73+ /// Return the list of catalan numbers between two ranks, inclusive
6874 /// </summary>
75+ /// <param name="fromRank"></param>
76+ /// <param name="toRank"></param>
77+ /// <returns></returns>
6978 public static List < BigInteger > GetRange ( uint fromRank , uint toRank )
7079 {
7180 var numbers = new List < BigInteger > ( ) ;
@@ -79,4 +88,4 @@ public static List<BigInteger> GetRange(uint fromRank, uint toRank)
7988 return numbers ;
8089 }
8190 }
82- }
91+ }
0 commit comments