Skip to content
Advertisement

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. abc = 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:

JavaScript

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).

for example:

  • input: 6 6 14 – output: 1 2 3
  • input: 1 2 3 – output : no values found

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

Advertisement

Answer

If you don’t want (or can) solve it analitically and the solutions are guaranteed to be integers (or you’re only looking for integer solutions) then this code will work and also handle negative numbers:

JavaScript
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement