44 * Wikipedia: https://en.wikipedia.org/wiki/Catalan_number
55 */
66
7- using System ;
87using System . Diagnostics ;
98using System . Collections . Generic ;
9+ using System . Numerics ;
1010
1111namespace Algorithms . Numeric
1212{
@@ -16,26 +16,28 @@ 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 Dictionary < uint , ulong > _catalanNumbers = new Dictionary < uint , ulong > ( ) { { 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- private static ulong _recursiveHelper ( uint rank )
24+ /// <param name="rank"></param>
25+ /// <returns></returns>
26+ 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
29- ulong number = 0 ;
30- uint lastRank = rank - 1 ;
31+ BigInteger number = 0 ;
32+ var lastRank = rank - 1 ;
3133
3234 for ( uint i = 0 ; i <= lastRank ; ++ i )
3335 {
34- ulong firstPart = _recursiveHelper ( i ) ;
35- ulong secondPart = _recursiveHelper ( lastRank - i ) ;
36+ var firstPart = _recursiveHelper ( i ) ;
37+ 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,44 +46,46 @@ private static ulong _recursiveHelper(uint rank)
4446 }
4547
4648 /// <summary>
47- /// Public API
49+ /// Public API.
4850 /// </summary>
49- public static ulong GetNumber ( uint rank )
51+ /// <param name="rank"></param>
52+ /// <returns></returns>
53+ 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>
60- public static ulong GetNumberByBinomialCoefficients ( uint rank )
64+ /// <param name="rank"></param>
65+ /// <returns></returns>
66+ public static BigInteger GetNumberByBinomialCoefficients ( uint rank )
6167 {
62- // Calculate value of 2nCn
63- var catalanNumber = BinomialCoefficients . Calculate ( 2 * rank , rank ) ;
64-
65- // return 2nCn/(n+1)
66- return Convert . ToUInt64 ( catalanNumber / ( rank + 1 ) ) ;
68+ // Calculate by binomial coefficient.
69+ return BinomialCoefficients . Calculate ( rank ) ;
6770 }
6871
6972 /// <summary>
70- /// Return the list of catalan numbers between two ranks, inclusive.
73+ /// Return the list of catalan numbers between two ranks, inclusive
7174 /// </summary>
72- public static List < ulong > GetRange ( uint fromRank , uint toRank )
75+ /// <param name="fromRank"></param>
76+ /// <param name="toRank"></param>
77+ /// <returns></returns>
78+ public static List < BigInteger > GetRange ( uint fromRank , uint toRank )
7379 {
74- List < ulong > numbers = new List < ulong > ( ) ;
80+ var numbers = new List < BigInteger > ( ) ;
7581
7682 if ( fromRank > toRank )
7783 return null ;
7884
79- for ( uint i = fromRank ; i <= toRank ; ++ i )
85+ for ( var i = fromRank ; i <= toRank ; ++ i )
8086 numbers . Add ( GetNumber ( i ) ) ;
8187
8288 return numbers ;
8389 }
84-
8590 }
86-
87- }
91+ }
0 commit comments