The optimal algorithm for calculating the Pythagorean triplet

I have this statement of the problem, where in the given expression x^2 + y^2 = cis the equation, you should optimally find the tuples (x,y)for which the equality holds.

Given the variable c, whose value is known, you need to find the values (x,y). Suppose, if you have c=0, then x=0and y=0. Suppose that you have c=2, then (x,y)- (-1,1) (1,-1), (-1,-1), (1,1). Now we have to find such values.

You just need to count the number of such tuples for a given value c.

Now I wrote the application as such:

int getCount(int c) {

  int count=0,temp=-1000;

  for(int i=-n;i<n-1;i++) {

    for(int j=-n,j<n;j++) {

      temp= i^2+j^2;

      if(temp==c) {
        count++;
        System.out.println(i+" "+j);
      }
    }
  }
}

Is there an optimal way to do this?

+3
5

, , , (1,1), , (x, y) (.. x < - → y).

, x^2 + y^2 = c, x <= sqrt(c) y <= sqrt(c). , x <= y, x <= sqrt(c/2) sqrt(c/2) <= y <= sqrt(c).

:

  • x^2+x^2 <= x^2 + y^2 = c x <= sqrt(c/2)
  • x <= y y^2 <= x^2 + y^2 = c, sqrt(c/2) <= y <= sqrt(c)

, |sqrt(c)-sqrt(c/2)| < |sqrt(c/2)| c, y sqrt(c/2) sqrt(c), x , x^2+y^2=c.

int print(x,y) {
  System.out.println(x+" "+y);
  System.out.println(-x+" "+y);
  System.out.println(-x+" "+-y);
  System.out.println(x+" "+y);
}

int getCount(int c) {
  if ( (c % 4) == 3)
    return 0;

  int count=0;
  for(int y = Math.sqrt(c/2) ; y <= Math.sqrt() ; y++) {
      int x = (int)Math.sqrt(c-y*y);
      if(x*x + y*y == c){
        count += 4;
        print(x,y);
        if(x != y) {
          count += 4;
          print(y,x);  
        }

      }
  }
}

, c = 1000*1000, 4n^2 (x,y), .. n = sqrt(c), 4 * 1000 * 1000 .

x sqrt (c) 1000 , 300 .

+4

{0 = > 0, 1 = > 1, 4 = > 2, 9 = > 3, 16 = > 4,..., 2 = > i}

[0, 1, 4, 9, 16,..., 2]

(i + 1) 2 > . , c.

+3

if ( (c % 4) == 3)
    return 0;

0 1 (mod 4), 3.

c - 0, 1, 2 (mod 4), x 0 sqrt (c), y , . 3 .

+1

c ^ 2 = a ^ 2 + b ^ 2 : m n - m > n:

  • a = m ^ 2 - n ^ 2
  • b = 2 * m * n
  • c = m ^ 2 + n ^ 2

m n c.

:

c = m^2 + n^2
=> m^2 = c - n^2
=> a = m^2 - n^2 = c - 2.n^2
=> n^2 = (c - a) / 2

:

a^2 + b^2 = c^2
=> b^2 = c^2 - a^2
=> (4.m^2.m^2) = c^2 - a^2
=> m^2 = c^2 - a^2 / 4.n^2
=> m^2 = (c - a)(c + a).2 / 4.(c - a)
=> m^2 = (c + a) / 2

m n - ( ), 0 <= m ^ 2 0 <= n ^ 2, , a:

0 <= (c + a) / 2  and 0 <= (c - a) / 2
=> -c <= a <= c

, c, [| -c, c |] ( ) m n. , , , , (c + a)/2 (c - a)/2 , (c + a) (c - a) 2, a 2.

  for (int a = -c; a <= c; a += 2) {
      double m2 = (c + a) * 0.5;
      double n2 = (c - a) * 0.5;

      double m_abs = Math.sqrt(m2);
      double n_abs = Math.sqrt(n2);

      if (Math.floor(m_abs) == m_abs &&
              Math.floor(n_abs) == n_abs) {

                      // sqrt(x^2) = abs(x^2) = +/- x
          System.out.println(Double.toString(-m_abs) + ", " + Double.toString(-n_abs));
          System.out.println(Double.toString(-m_abs) + ", " + Double.toString(n_abs));
          System.out.println(Double.toString(m_abs) + ", " + Double.toString(-n_abs));
          System.out.println(Double.toString(m_abs) + ", " + Double.toString(n_abs));
      }
  }
+1
source

I would say that this is a duplicate of this question . The question here is somewhat more general, i.e. The question does not require c to be a square, but this is also addressed in previous answers. And here is also a good description of the factorization of Gaussian integers , which is not fully described in another question.

+1
source

Source: https://habr.com/ru/post/1769695/


All Articles