Program for nth Fuss–Catalan Number
Last Updated :
08 Mar, 2022
Fuss–Catalan Numbers are a generalization of Catalan numbers that uses triplets instead of pairs.
The Fuss-Catalan Numbers can be represented by a Series with the formula:
The first few Fuss–Catalan Numbers are
1, 1, 3, 12, 55, 273, 1428, 7752, 43263, 246675………..
for n = 0, 1, 2, 3, … respectively
Applications of Fuss-Catalan number:
- Count the number of ways to place parentheses among of 2n+1 numbers to be grouped three at a time.
Example: There are 3 ways to parenthesize {1, 2, 3, 4, 5} as triplets:
{{1, 2, 3}, 4, 5}, {1, {2, 3, 4}, 5}, {1, 2, {3, 4, 5}}
- Count the number of complete ternary trees with n internal nodes.
-
- Count the number of paths of length 3n through a 2n-by-n grid that does not cross above the main diagonal
Example: There are 3 paths from (0, 0) to (4, 2) that don’t cross above the diagonal:
-
- and many more. Please refer this link for more applications
Implementation of Fuss-Catalan number:
C++
#include <iostream>
using namespace std;
unsigned long int binomialCoeff(unsigned int n,
unsigned int k)
{
unsigned long int res = 1;
if (k > n - k)
k = n - k;
for ( int i = 0; i < k; ++i) {
res *= (n - i);
res /= (i + 1);
}
return res;
}
unsigned long int Fuss_catalan(unsigned int n)
{
unsigned long int c = binomialCoeff(3 * n, n);
return c / (2 * n + 1);
}
int main()
{
for ( int i = 0; i < 10; i++)
cout << Fuss_catalan(i) << " " ;
return 0;
}
|
Java
class GFG
{
static int binomialCoeff( int n, int k)
{
int res = 1 ;
if (k > n - k)
k = n - k;
for ( int i = 0 ; i < k; ++i)
{
res *= (n - i);
res /= (i + 1 );
}
return res;
}
static int Fuss_catalan( int n)
{
int c = binomialCoeff( 3 * n, n);
return c / ( 2 * n + 1 );
}
public static void main(String []args)
{
for ( int i = 0 ; i < 10 ; i++)
System.out.print(Fuss_catalan(i) + " " );
}
}
|
Python3
def binomialCoeff(n, k) :
res = 1 ;
if (k > n - k) :
k = n - k;
for i in range (k) :
res * = (n - i);
res / / = (i + 1 );
return res;
def Fuss_catalan(n) :
c = binomialCoeff( 3 * n, n);
return c / / ( 2 * n + 1 );
if __name__ = = "__main__" :
for i in range ( 10 ) :
print (Fuss_catalan(i), end = " " );
|
C#
using System;
class GFG
{
static int binomialCoeff( int n, int k)
{
int res = 1;
if (k > n - k)
k = n - k;
for ( int i = 0; i < k; ++i)
{
res *= (n - i);
res /= (i + 1);
}
return res;
}
static int Fuss_catalan( int n)
{
int c = binomialCoeff(3 * n, n);
return c / (2 * n + 1);
}
public static void Main(String []args)
{
for ( int i = 0; i < 10; i++)
Console.Write(Fuss_catalan(i) + " " );
}
}
|
Javascript
<script>
function binomialCoeff(n, k)
{
var res = 1;
if (k > n - k)
k = n - k;
for ( var i = 0; i < k; ++i) {
res *= (n - i);
res = parseInt(res / (i + 1));
}
return res;
}
function Fuss_catalan(n)
{
var c = binomialCoeff(3 * n, n);
return parseInt(c / (2 * n + 1));
}
for ( var i = 0; i < 10; i++)
document.write(Fuss_catalan(i)+ " " );
</script>
|
Output:
1 1 3 12 55 273 1428 7752 43263 246675
Time Complexity: O(n)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...