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++
// C++ program for binomial coefficients #include <stdio.h> // Function to print binomial table int printbinomial( int max) { for ( int m = 0; m <= max; m++) { printf ( "%2d" , m); int binom = 1; for ( int x = 0; x <= m; x++) { // B(m, x) is 1 if either m or x // is 0. if (m != 0 && x != 0) // Otherwise using recursive formula // B(m, x) = B(m, x - 1) * (m - x + 1) / x binom = binom * (m - x + 1) / x; printf ( "%4d" , binom); } printf ( "\n" ); } } // Driver Function int main() { int max = 10; printbinomial(max); return 0; } |
Java
// Java program for // binomial coefficients import java.io.*; class GFG { // Function to print // binomial table 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++) { // B(m, x) is 1 if either // m or x is 0. if (m != 0 && x != 0 ) // Otherwise using // recursive formula // B(m, x) = B(m, x - 1) * // (m - x + 1) / x binom = binom * (m - x + 1 ) / x; System.out.print(binom + " " ); } System.out.println(); } } // Driver Code public static void main (String[] args) { int max = 10 ; printbinomial(max); } } // This code is contributed // by akt_mit |
Python3
# Python3 program for binomial # coefficients # Function to print binomial table def printbinomial ( max ): for m in range ( max + 1 ): print ( '% 2d' % m, end = ' ' ) binom = 1 for x in range (m + 1 ): # B(m, x) is 1 if either m # or x is 0. if m ! = 0 and x ! = 0 : # Otherwise using recursive # formula # B(m, x) = B(m, x - 1) * # (m - x + 1) / x binom = binom * (m - x + 1 ) / x print ( '% 4d' % binom, end = ' ' ) print ( "\n" , end = '') # Driver Function max = 10 printbinomial( max ) # This code is contributed by "Sharad_bhardwaj". |
C#
// C# program for binomial coefficients using System; public class GFG { // Function to print binomial table 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++) { // B(m, x) is 1 if either m // or x is 0. if (m != 0 && x != 0) // Otherwise using recursive formula // B(m, x) = B(m, x - 1) * (m - x + 1) / x binom = binom * (m - x + 1) / x; Console.Write(binom + " " ); } Console.WriteLine(); } } // Driver Function static public void Main() { int max = 10; printbinomial(max); } } // This code is contributed by vt_m. |
PHP
<?php // PHP program for // binomial coefficients // Function to print // binomial table function printbinomial( $max ) { for ( $m = 0; $m <= $max ; $m ++) { echo $m ; $binom = 1; for ( $x = 0; $x <= $m ; $x ++) { // B(m, x) is 1 if either // m or x is 0. if ( $m != 0 && $x != 0) // Otherwise using // recursive formula // B(m, x) = B(m, x - 1) * // (m - x + 1) / x $binom = $binom * ( $m - $x + 1) / $x ; echo " " , $binom , " " ; } echo "\n" ; } } // Driver Code $max = 10; printbinomial( $max ); // This code is contributed by aj_36 ?> |
Javascript
<script> // Javascript program for // binomial coefficients // Function to print // binomial table function printbinomial(max) { for (let m = 0; m <= max; m++) { document.write(m); let binom = 1; for (let x = 0; x <= m; x++) { // B(m, x) is 1 if either // m or x is 0. if (m != 0 && x != 0) // Otherwise using // recursive formula // B(m, x) = B(m, x - 1) * // (m - x + 1) / x binom = binom * (m - x + 1) / x; document.write( " " + binom + " " ); } document.write( "<br>" ); } } // Driver Code let max = 10; printbinomial(max); // This code is contributed by _saurabh_jaiswal </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)
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!