How to find all possible values of four variables when squared sum to N?

后端 未结 4 497
醉酒成梦
醉酒成梦 2020-12-28 23:39

A^2+B^2+C^2+D^2 = N Given an integer N, print out all possible combinations of integer values of ABCD which solve the equation.

<
4条回答
  •  梦谈多话
    2020-12-29 00:15

    nebffa has a great answer. one suggestion:

     step 3a:  max_b = min(a, floor(square_root(n - a^2))) // since b <= a
    

    max_c and max_d can be improved in the same way too.

    Here is another try:

    1. generate array S: {0, 1, 2^2, 3^2,.... nr^2} where nr = floor(square_root(N)). 
    

    now the problem is to find 4 numbers from the array that sum(a, b,c,d) = N;

    2. according to neffa's post (step 1a & 1b), a (which is the largest among all 4 numbers) is between [nr/2 .. nr]. 
    

    We can loop a from nr down to nr/2 and calculate r = N - S[a]; now the question is to find 3 numbers from S the sum(b,c,d) = r = N -S[a];

    here is code:

    nr = square_root(N);
    S = {0, 1, 2^2, 3^2, 4^2,.... nr^2};
    for (a = nr; a >= nr/2; a--)
    {
       r = N - S[a];
       // it is now a 3SUM problem
       for(b = a; b >= 0; b--)
       {
          r1 = r - S[b];
          if (r1 < 0) 
              continue;
    
          if (r1 > N/2) // because (a^2 + b^2) >= (c^2 + d^2)
              break;
    
          for (c = 0, d = b; c <= d;)
          {
              sum = S[c] + S[d];
              if (sum == r1) 
              {
                   print a, b, c, d;
                   c++; d--;
              }
              else if (sum < r1)
                   c++;
              else
                   d--;
          }
       }
    }
    

    runtime is O(sqare_root(N)^3).

    Here is the test result running java on my VM (time in milliseconds, result# is total num of valid combination, time 1 with printout, time2 without printout):

    N            result#  time1     time2
    -----------  -------- --------  -----------
      1,000,000   1302       859       281
     10,000,000   6262     16109      7938
    100,000,000  30912    442469    344359
    

提交回复
热议问题