4
4
* Wikipedia: https://en.wikipedia.org/wiki/Catalan_number
5
5
*/
6
6
7
- using System ;
8
7
using System . Diagnostics ;
9
8
using System . Collections . Generic ;
9
+ using System . Numerics ;
10
10
11
11
namespace Algorithms . Numeric
12
12
{
@@ -16,26 +16,28 @@ 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 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 } } ;
20
20
21
21
/// <summary>
22
- /// Helper function .
22
+ /// Helper method .
23
23
/// </summary>
24
- private static ulong _recursiveHelper ( uint rank )
24
+ /// <param name="rank"></param>
25
+ /// <returns></returns>
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
- ulong number = 0 ;
30
- uint lastRank = rank - 1 ;
31
+ BigInteger number = 0 ;
32
+ var lastRank = rank - 1 ;
31
33
32
34
for ( uint i = 0 ; i <= lastRank ; ++ i )
33
35
{
34
- ulong firstPart = _recursiveHelper ( i ) ;
35
- ulong secondPart = _recursiveHelper ( lastRank - i ) ;
36
+ var firstPart = _recursiveHelper ( i ) ;
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,44 +46,46 @@ private static ulong _recursiveHelper(uint rank)
44
46
}
45
47
46
48
/// <summary>
47
- /// Public API
49
+ /// Public API.
48
50
/// </summary>
49
- public static ulong GetNumber ( uint rank )
51
+ /// <param name="rank"></param>
52
+ /// <returns></returns>
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>
60
- public static ulong GetNumberByBinomialCoefficients ( uint rank )
64
+ /// <param name="rank"></param>
65
+ /// <returns></returns>
66
+ public static BigInteger GetNumberByBinomialCoefficients ( uint rank )
61
67
{
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 ) ;
67
70
}
68
71
69
72
/// <summary>
70
- /// Return the list of catalan numbers between two ranks, inclusive.
73
+ /// Return the list of catalan numbers between two ranks, inclusive
71
74
/// </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 )
73
79
{
74
- List < ulong > numbers = new List < ulong > ( ) ;
80
+ var numbers = new List < BigInteger > ( ) ;
75
81
76
82
if ( fromRank > toRank )
77
83
return null ;
78
84
79
- for ( uint i = fromRank ; i <= toRank ; ++ i )
85
+ for ( var i = fromRank ; i <= toRank ; ++ i )
80
86
numbers . Add ( GetNumber ( i ) ) ;
81
87
82
88
return numbers ;
83
89
}
84
-
85
90
}
86
-
87
- }
91
+ }
0 commit comments