0

My problem is: enter image description here

My math formula is: enter image description here In this case X = N; Y = L;U = K;

    public class Play {

    public static void main(String args[]) {
         //n!(n−k−1)!
        int n = 10;
        int k =2;
        int l = 12;


        long result;
        result = (calculaFator(n) / calculaFator(n-k-1));
        result= (long) (result * Math.pow((n-k),(l-k)-1));
        System.out.println(result);


    }

    public static long calculaFator(long x) {
        long f = x;

        while (x > 1) {

            f = f * (x - 1);

            x--;
        }
        return f;
    }
}

It should be 721599986, but it is giving me 96636764160

I have some samples:

With n=10, k=2, l=12 it should be 721599986

With n=10, k=2, l=16 it should be 626284798

With n=10, k=1, l=20 it should be 674941304

With n=5, k=2, l=8 it should be 10800

1 Answers1

0

The java codes is working according to your stated formula. It seems like the formula is wrong rather than the codes. (or expected results or your x,u,y mapping to n,l,k is incorrect?)

int x = 10;
int u = 2;
int y = 12;
long numerator = calculaFator(x);
long denominator = calculaFator(x - u - 1);

int xu1 = x - u - 1;
long result = numerator / denominator;
System.out.println();
System.out.println(x + "!= numerator: " + numerator);  //10!= numerator: 3_628_800 
System.out.println(xu1 + "!= denominator: " + denominator); //7!= denominator: 5_040 
System.out.println("result1: " + result); //result1: 720 (correct)

int xu = x - u;
int yu1 = y - u - 1;
double remainderPlaylist = Math.pow(xu, yu1);
System.out.println(xu + "^" + yu1 + " = " + remainderPlaylist);//8^9 = 1.34217728E8 
System.out.println(xu + "^" + yu1 + " = " + (long) remainderPlaylist);//8^9 = 134_217_728   (correct)

long mul = (long) (result * remainderPlaylist);
System.out.println(result + "x" + (long)remainderPlaylist + " = " + mul); //720x134_217_728 = 96_636_764_160  (mathematically correct)
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Angel Koh
  • 12,479
  • 7
  • 64
  • 91