Program for Binomial Coefficients table
Given an integer max, print Binomial Coefficients table that prints all binomial coefficients B(m, x) where m and x vary from 0 to max
Example :
Input : max = 3
Output :
0 1
1 1 1
2 1 2 1
3 1 3 3 1
The easiest way to explain what binomial coefficients is to say that they count certain ways of grouping items. Specifically, the binomial coefficient B(m, x) counts the number of ways to form an unordered collection of k items chosen from a collection of n distinct items.
Binomial coefficients are used in the study of binomial distributions and multicomponent redundant systems. It is given by
Example :
Compute B(7, 3) where m = 7 and x = 1
(7!/3!(7-3)!)7
= 7!/3!*4!
= (7*6*5*4*3*2*1)/(3*2*1)*(4*3*2*1)
= 35
A table of binomial coefficients is required to determine the binomial coefficient for any value m and x.
Problem Analysis :
The binomial coefficient can be recursively calculated as follows –
further,
That is the binomial coefficient is one when either x is zero or m is zero. The program prints the table of binomial coefficients for .
C++
#include <stdio.h>
int printbinomial( int max)
{
for ( int m = 0; m <= max; m++) {
printf ( "%2d" , m);
int binom = 1;
for ( int x = 0; x <= m; x++) {
if (m != 0 && x != 0)
binom = binom * (m - x + 1) / x;
printf ( "%4d" , binom);
}
printf ( "\n" );
}
}
int main()
{
int max = 10;
printbinomial(max);
return 0;
}
|
Java
import java.io.*;
class GFG
{
static void printbinomial( int max)
{
for ( int m = 0 ; m <= max; m++)
{
System.out.print(m + " " );
int binom = 1 ;
for ( int x = 0 ; x <= m; x++)
{
if (m != 0 && x != 0 )
binom = binom * (m - x + 1 ) / x;
System.out.print(binom + " " );
}
System.out.println();
}
}
public static void main (String[] args)
{
int max = 10 ;
printbinomial(max);
}
}
|
Python3
def printbinomial ( max ):
for m in range ( max + 1 ):
print ( '% 2d' % m, end = ' ' )
binom = 1
for x in range (m + 1 ):
if m ! = 0 and x ! = 0 :
binom = binom * (m - x + 1 ) / x
print ( '% 4d' % binom, end = ' ' )
print ( "\n" , end = '')
max = 10
printbinomial( max )
|
C#
using System;
public class GFG {
static void printbinomial( int max)
{
for ( int m = 0; m <= max; m++) {
Console.Write(m + " " );
int binom = 1;
for ( int x = 0; x <= m; x++) {
if (m != 0 && x != 0)
binom = binom * (m - x + 1) / x;
Console.Write(binom + " " );
}
Console.WriteLine();
}
}
static public void Main()
{
int max = 10;
printbinomial(max);
}
}
|
PHP
<?php
function printbinomial( $max )
{
for ( $m = 0; $m <= $max ; $m ++)
{
echo $m ;
$binom = 1;
for ( $x = 0; $x <= $m ; $x ++)
{
if ( $m != 0 && $x != 0)
$binom = $binom * ( $m - $x + 1) / $x ;
echo " " , $binom , " " ;
}
echo "\n" ;
}
}
$max = 10;
printbinomial( $max );
?>
|
Javascript
<script>
function printbinomial(max)
{
for (let m = 0; m <= max; m++)
{
document.write(m);
let binom = 1;
for (let x = 0; x <= m; x++)
{
if (m != 0 && x != 0)
binom = binom * (m - x + 1) / x;
document.write( " " + binom + " " );
}
document.write( "<br>" );
}
}
let max = 10;
printbinomial(max);
</script>
|
Output :
0 1
1 1 1
2 1 2 1
3 1 3 3 1
4 1 4 6 4 1
5 1 5 10 10 5 1
6 1 6 15 20 15 6 1
7 1 7 21 35 35 21 7 1
8 1 8 28 56 70 56 28 8 1
9 1 9 36 84 126 126 84 36 9 1
10 1 10 45 120 210 252 210 120 45 10 1
Time complexity: O(max2) for given max
Auxiliary space: O(1)
Last Updated :
30 Sep, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...