vendredi 14 mai 2021

how do i find the values of A,B and C in java

given the following values X,Y,Z, such as (0≤X,Y,Z≤10000) with considering the following three equations :

  1. a+b+c = X
  2. a * b * c = Y
  3. a^2 + b^2+ c^2 = Z

i need to find values of a, b and c, I wrote something like this in java:

    int[] values = new int[3];

    for (int a = 0 ; x<= 600 ; x++){
        for(int b =0 ; y<=600 ; y++){
            for(int c=0 ; z<=600 ; z++){
                int pow = (int) Math.pow(a,2) + (int) Math.pow(b,2)+ (int) Math.pow(c,2); 
                if((x+y+z == X) && (x*y*z==Y) && (pow==Z)){
                    values[0]=a ; values[1]=b; values[2]=c;
                    Arrays.sort(values);
                    System.out.println(values[0]+" "+values[1]+" "+values[2]);
                    System.exit(0);
                }
                
            }
        }
    }
    
    System.out.println("no values found");

but its a naïve solution because the values of a,b and c can be zero, positive or negative numbers and must be distinct (a≠b≠c).

I have been struggling for days, how do I start the loop & inner loops so it can check negative numbers and (a≠b≠c) to satisfy the three equations.

for example:

  • input: 6 6 14 - output: 1 2 3
  • input: 1 2 3 - output : no values found
  • input: 0 240 48 - output: -4 -6 10

input represent x,y,z and output represent a,b,c. can someone please put me on the right path and help me.

Aucun commentaire:

Enregistrer un commentaire