-1

Task is to calculate expression for natural numbers entered.

enter image description here

I know I should calculate binominal coefficient here right? enter image description here

Also I know that (-1)^p determines whether this array is decrementing or incrementing, but don't know how to use p in my code I am not quite sure how to put it all together, this is what I came up with so far and it is really nothing special as I still can't grasp on the idea of how to write this in program.

public static int calculateExpression(int n, int k,int p) {

      if(k<0 || n<k)
         {
             return 0;
         }
    // Find factorial of n

    int n_fac = 1;
        for (int j = 1; j <= n; j++) {
            n_fac = n_fac * j;

        }

    // Find factorial of k

    int k_fac = 1;
        for(int i = 1; i<=k; i++) {
            k_fac = k_fac * i;

        }

        // Find n-k fac
        int n_k = n-k;
        int n_k_fac = 1;
            for(int l = 1; l<=n_k;l++) {
                n_k_fac*=l;
            }

        //      n/k = n!/k!(n-k)!

        double resultOf_n_kDivision = n_fac/k_fac*n_k_fa;

    System.out.println(resultOf_n_kDivision);
    return n_k_fac;



}
Temp034
  • 141
  • 11

4 Answers4

1

The factorial function is a very fast-growing one, so calculating the numerator and denominator separately may not be a good idea, as it may lead to overflow for even relatively small values of n.

Let's look at an iterative method for calculating the coefficient:

enter image description here

We see that we can calculate the next coefficient of the row if we know the current one. Thus we can incrementally calculate each term in S, while being less concerned about overflow problems.

static int calculateExpression(int n, int k, int p)
{
   // first term of the row is (n, 0) = 1
   int binom = 1;

   // Iteratively find (n, k)
   for (int i = 0; i < k; i++)
      binom = (binom * (n - i)) / (i + 1);

   // sum the series
   int S = binom;
   for (int i = 0; i < p; i++) {
      // "trick": multiply with a minus sign each time to flip the sign
      binom = (-binom * (n - k - i)) / (k + i + 1);
      S += binom;
   }

   return S;
}

UPDATE: Parallel numerical tests:

n   k   p | orig  new
----------------------
5   3   2 | 6     6
10  4   1 | -42   -42
12  3   7 | 44    44
15  8   6 | 3433  8     // integer overflow occurred with the original method

As you can see the two functions were consistent until the last line with n = 15, as 15! = 1307674368000 is much bigger than the maximum positive value of int in most implementations of Java (32-bit).

meowgoesthedog
  • 14,670
  • 4
  • 27
  • 40
0

Use abstraction for better tackling problems; define fac and over. Then the problem becomes:

public static int calculateExpression(int n, int k,int p) {
    int sum = 0;
    int minus1toP = 1;
    for (int i = 0; i <= p; i++) {
        sum += minus1toP * over(n, ...);
        minus1toP = -minus1toP;
    }
    return sum;
}

static int over(int n, int k) {
    return fac(n) / fac(k) / fac(n - k);
}

static int fac(int n) {
    int f = 1;
    for(int i = 2; i <= n; i++) {
        f *= i;
    }
    return f;
}

I did not give the entire solution (...), but maybe too much already.

Joop Eggen
  • 107,315
  • 7
  • 83
  • 138
0

I did not really get your question, but you can just use this.

    public static double combination(int n, int k)
    {
        double nFactorial = getFactorialFromNToK(n, k);
        double kFactorial = getFactorialFromNToK(k, 1);

        return nFactorial / kFactorial;
    }

    public static double getFactorialFromNToK(double n, double k)
    {
        double factorial = 1;

        for (; n - k + 1 > 0; n--)
        {
            factorial *= n;
        }

        return factorial;
    }

This is the evaluation of nCk for the coef of a term in the binomial expansion.

If nCn is a term in the expansion, then it converges and if it does not exist as term in the expansion, then it will not converge. So if it is a natural number expansion, then it will always converge.

staa99
  • 191
  • 1
  • 2
  • 13
0

A better solution is to use the lngamma function instead of factorial. It's a more efficient way to calculate factorials. The natural log means that dividing large numbers will be less of a problem.

duffymo
  • 305,152
  • 44
  • 369
  • 561