0

I'm trying to write a code for the binomial coefficient. I have already made the method for the binomial coefficient but my program have to initialize an array which should display the binomial coefficient but I am only allowed to use the binomial method ({1,1,1,1,2,1}, for example). Here is my code. I really have no idea how to make this work under these conditions.

public static int faculty(int factorial) {
    int CalculatedFactorial = 1;
    for (int i=2;i<=factorial;i++)
    {
        CalculatedFactorial = CalculatedFactorial*i;
    }
    return CalculatedFactorial;
}

public static int binomialCoefficient(int n,int k) {
    int num = faculty(n);
    int denum = faculty(k)*faculty(n-k);
    int BinomCoefResult=(int)(num/denum);
    return BinomCoefResult;
}

public static int[] triangleOfBinCoeff(int heightOfTriangle) {
    int [] arrayTr;
    for(int i=0;i<heightOfTriangle;i++)
    {
        for (int m=0;m<=i;m++)
        {
            arrayTr[i]= PascalsTriangle.binomialCoefficient();
        }
    }
    return arrayTr;
}

I'd really appreciate any help.

  • please explain "but I am only allowed to use the binomial method." Are you saying that the current code works but you are not allowed to do it that way? Or if not, what goes wrong with the current code? – Jeremy Kahan Nov 15 '19 at 19:24
  • A part of your problem description isn't clear "binomial method ({1,1,1,1,2,1}," . Give a complete example, for what input and what output you are expecting. – User_67128 Nov 15 '19 at 19:25
  • are you perhaps saying that binomialCoefficient should not call factorial but rather at either end of a row be 1 and otherwise call itself as the sum of binomialCoefficient of the 2 entries above it in the triangle (or look those up in the array?)? – Jeremy Kahan Nov 15 '19 at 19:33
  • 1
    @ManojBanik I suspect that that array is the first three rows of Pascal's triangle, concatenated. – erickson Nov 15 '19 at 19:33
  • You can see a details solution here: https://www.geeksforgeeks.org/pascal-triangle/ And to find the nCr you don't need to find factorial of three values. nCr = (n * [n-1] * ... *[n-r+1]) / (1 * 2 *3 * ... *r). This formula will save lots of calculations. – User_67128 Nov 15 '19 at 19:33
  • @JeremyKahan Everything works except for arrayTr[i]= PascalsTriangle.binomialCoefficient(); i mean i have to have an output of an array which have to be defined with the method binomialCoefficient() – Eli Ivanova Nov 15 '19 at 19:41
  • @ManojBanik input triangleOfBinCoeff(2) output {1,1,1,1,2,1} – Eli Ivanova Nov 15 '19 at 19:43

2 Answers2

0

One problem is your calculation of the factorial. Because the factorial grows so quickly, computing 13! will overflow the capacity of an int. You should look for a more efficient way to calculate n!/(n - k)!, since the denominator will usually be large enough to keep the overall value of the expression small.

In your method triangleOfBinCoeff(), there are a number of problems.

public static int[] triangleOfBinCoeff(int heightOfTriangle) {
    int [] arrayTr;
    for(int i=0;i<heightOfTriangle;i++)
    {
        for (int m=0;m<=i;m++)
        {
            arrayTr[i]= PascalsTriangle.binomialCoefficient();
        }
    }
    return arrayTr;
}

You don't allocate an array for arrayTr. You either need to precompute the necessary size, and initialize arrayTr, or use a dynamically allocated structure like List<Integer> instead.

Then you don't pass any arguments to binomialCoefficient(), and the array element, arrayTr[i] to which to try to assign the result is the row number, and that is incorrect. You want to use either the column in the last row (m), or perhaps you are building the entire triangle and need to track the count of elements written previously.

erickson
  • 265,237
  • 58
  • 395
  • 493
0

You can use the following function:

public static int[] triangleOfBinCoeff(int n)
{
   int arrSize = getCoeffCount(n);
   int[] result = new int[arrSize];

   for (int k = 0; k <= n - 1; k++)
   {
      int posC0k = getCoeffPos(0, k);
      int posCkk = getCoeffPos(k, k);
      result[posC0k] = 1;
      result[posCkk] = 1;

      if (k > 1)
      for (int j = 1; j < k; j++)
      {
         result[getCoeffPos(j, k)] = result[getCoeffPos(j-1, k-1)] + result[getCoeffPos(j, k-1)];
      }
   }

   return result;
}

private static int getCoeffCount(int n)
{
   return n * (n + 1) / 2;
}

private static int getCoeffPos(int k, int n)
{
   return getCoeffCount(n) + k;
}

...
int[] coeffs = triangleOfBinCoeff(5); // (1,1,1,1,2,1,1,3,3,1,1,4,6,4,1)

It based on the C(k, n) = C(k-1, n-1) + C(k, n-1) recurrence relation. As @erickson said, the factorial function grows very quickly. So, it is ineffective to use the definition of binomial coefficient as C(k, n) = n!/k!(n-k)!.

SternK
  • 11,649
  • 22
  • 32
  • 46