# Complexity of Binomial Coefficient Calculation

4 Pages1164 Words159 Views
|
|
|
~~~~~~~~~~~~~~~~~~Answer 1: ~~~~~~~~~~~~~~~~~Complexity = O(n^2)Function to compute sum is (n/2)^2;Proof : 1 + 3 + 5 + 7 + 9 + 11 + 2(n-1) = n^2so : 1 + 3 + 5 + 7 + 9 + n-1 = (n/2)^2;Some Example : 10 - 2515 - 49100 - 25001000 – 250000Prog :publicclass Test2 {publicstaticvoid main(String[] args) {System.out.println(func(10000));}publicstaticint func(intn) {intsum = 0;n = n - ( n % 2 );for (intk = 1; k < n; k += 2)for (intj = 0; j < k; ++j)++sum;returnsum;}}~~~~~~~~~~~~~~~Answer 2: ~~~~~~~~~~~~~~~~~~~~No of comparison is same in the all the cases.It will be 2(n-1)Complexity: O(n).As Every element is getting compared 2 times except the first element A[0].So Complexity is O (2(n-1)) which is equivalent to O (n)~~~~~~~~~~~~~~~ Answer 3: ~~~~~~~~~~~~~~~~~~~~
It will be printed 55 Times.Make the recursion tree to understand more f(4*4*4*4*4) —1. f(4*4*4*4*2) — so on2. f(4*4*4*4*1) — so onProgram to verify publicclass Test1 {privatestaticintcount = 0;publicstaticvoid main(String[] args) {System.out.println("Starting....");fun(4*4*4*4*4);System.out.println(count);}privatestaticvoid fun(intn) {if(n<2) {return;}if(n==2) {count++;System.out.println("Hii");}fun(n/2);fun(n/4);}}~~~~~~~~~~~~ Answer 4 : ~~~~~~~~~~~~~~~~~~~~~~Binomail Cofficient:C(n, k) = C(n-1 , k-1) + C(n-1, k) C(n,0) = C(n,n) = 1 // Base conditionRecursive Function:int binomialCoeff(int n, int k){// Base Cases

## End of preview

Want to access all the pages? Upload your documents or become a member.

Related Documents

### Support

#### +1 306 205-2269

Chat with our experts. we are online and ready to help.