-1

I am trying to solve a problem that I need to get value of three unknowns(x,y,z) knowing some info. their summation is equal to 70, x^2 + y^2 = z^2 and x < y < z.

Answer should be x = 20, y = 21, z = 29

I tried to solve it as two equations in three unknowns but I failed. Any hints to get the solution ? I want to find an algorithm or equation to build a java code that solve this problem

Rory Daulton
  • 21,934
  • 6
  • 42
  • 50

3 Answers3

1

I'll assume that x, y, and z must be positive integers, since removing the integers restriction allows infinitely many solutions. Here is an algorithm--I'll leave the code to you.

Your second equation x^2 + y^2 = z^2 means that x, y, and z form a Pythagorean triple. All solutions to that equation have the form

x = k(m^2 - n^2), y = 2kmn, z = k(m^2 + n^2)

(with possibly x and y swapped) where m, n, and k are positive integers, m > n, one of m and n is even and the other is odd, and (m, n) are relatively prime. You can drop those last two restrictions on m and n, which is to make the triples have unique representation.

Your third limitation x < y < z merely makes a unique triple from the three values. Importantly, your first restriction x + y + z = 70 means that your solution has "small" values.

So in your code, vary the three parameters k, m, and n. There are only finitely many values that allow the sum of x, y, and z to be less than or equal 70, which places limits on k, m, and n. Find the ones that equal make the sum of x, y, and z to be 70. You can cut the number of trials in half by not letting m and n be both even or both odd. You can also avoid explicitly varying k by varying only m and n and calculating what k should be, since each of x, y, z vary proportionally with k, and accept only integral k.

This is somewhat of a brute-force solution, but it is easy to program and will be faster than just trying all values of x, y, and z.


EDIT: I now see that x, y, and z may also be zero. That theoretically means that you need to test for x = 0, but that is clearly impossible here since then y^2 = z^2 which contradicts y < z. So no change is needed to my algorithm.

Rory Daulton
  • 21,934
  • 6
  • 42
  • 50
  • I did not get that yet, how can I vary the values of m,n and k without looping on all values ? this will cost me to make three loops or what ? I do not understand – Roony Saeed Aug 09 '17 at 01:30
  • @RoonySaeed: You can loop only on m and n, not both even or both odd--for each pair, calculate what k must be and accept the triple if k is an integer. There is some looping here--this is a programming forum, did you expect to avoid all loops? You do not give any such requirement in your question. My algorithm is just much faster than simply looping on x, y, and z. It is possible to make just one loop, varying m and n in that loop, if you like. – Rory Daulton Aug 09 '17 at 08:50
  • Thanks a lot for your efforts, it helped me a lot. I solved it. :) – Roony Saeed Aug 10 '17 at 03:46
1

Expanding on @RoryDaulton's answer, taking x = k(m^2 - n^2), y = 2kmn and z = k(m^2 + n^2) and applying the sum constraint gives us

2*k*m*(m + n) = 70

Or

k * m * (m + n) = 35 = 7 * 5 = 35 * 1

The important thing to note is that the RHS of the above has only two unique factors; the LHS has three. Thus at least one factor of the LHS (k, m, m + n) must be 1.

Since m and n are unique positive integers, m + n will always be greater than 1. Thus,

k = 1 or m = 1

And the only possible values for the remaining LHS factors are 7 and 5 or 35 and 1.

This makes the problem much easier to brute force.

EvilTak
  • 7,091
  • 27
  • 36
0

I have solved the question and I want to thank all people who helped me.

This is My code to solve the problem

int x,y,z;
  long mul=0;   
for(int n=1;n<=sum;n++){
for (int m=2;m<=sum;m++){
    x= (int) ((Math.pow(m,2)) - (Math.pow(n,2)));
    y= 2*m*n;
    z= (int) ((Math.pow(m,2)) + (Math.pow(n,2)));
 if(x+y+z == sum){
     mul = x*z*y;
 }
}}
   return mul; }}