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

后端 未结 4 498
醉酒成梦
醉酒成梦 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:32

    It seems as though all integers can be made by such a combination:

    0 = 0^2 + 0^2 + 0^2 + 0^2
    1 = 1^2 + 0^2 + 0^2 + 0^2
    2 = 1^2 + 1^2 + 0^2 + 0^2
    3 = 1^2 + 1^2 + 1^2 + 0^2
    4 = 2^2 + 0^2 + 0^2 + 0^2, 1^2 + 1^2 + 1^2 + 1^2 + 1^2
    5 = 2^2 + 1^2 + 0^2 + 0^2
    6 = 2^2 + 1^2 + 1^2 + 0^2
    7 = 2^2 + 1^2 + 1^2 + 1^2
    8 = 2^2 + 2^2 + 0^2 + 0^2
    9 = 3^2 + 0^2 + 0^2 + 0^2, 2^2 + 2^2 + 1^2 + 0^2
    10 = 3^2 + 1^2 + 0^2 + 0^2, 2^2 + 2^2 + 1^2 + 1^2
    11 = 3^2 + 1^2 + 1^2 + 0^2
    12 = 3^2 + 1^2 + 1^2 + 1^2, 2^2 + 2^2 + 2^2 + 0^2
    .
    .
    .
    

    and so forth

    As I did some initial working in my head, I thought that it would be only the perfect squares that had more than 1 possible solution. However after listing them out it seems to me there is no obvious order to them. However, I thought of an algorithm I think is most appropriate for this situation:

    The important thing is to use a 4-tuple (a, b, c, d). In any given 4-tuple which is a solution to a^2 + b^2 + c^2 + d^2 = n, we will set ourselves a constraint that a is always the largest of the 4, b is next, and so on and so forth like:

    a >= b >= c >= d
    

    Also note that a^2 cannot be less than n/4, otherwise the sum of the squares will have to be less than n.

    Then the algorithm is:

    1a. Obtain floor(square_root(n)) # this is the maximum value of a - call it max_a
    1b. Obtain the first value of a such that a^2 >= n/4 - call it min_a
    2. For a in a range (min_a, max_a)
    

    At this point we have selected a particular a, and are now looking at bridging the gap from a^2 to n - i.e. (n - a^2)

    3. Repeat steps 1a through 2 to select a value of b. This time instead of finding
    floor(square_root(n)) we find floor(square_root(n - a^2))
    

    and so on and so forth. So the entire algorithm would look something like:

    1a. Obtain floor(square_root(n)) # this is the maximum value of a - call it max_a
    1b. Obtain the first value of a such that a^2 >= n/4 - call it min_a
    2. For a in a range (min_a, max_a)
    3a. Obtain floor(square_root(n - a^2))
    3b. Obtain the first value of b such that b^2 >= (n - a^2)/3
    4. For b in a range (min_b, max_b)
    5a. Obtain floor(square_root(n - a^2 - b^2))
    5b. Obtain the first value of b such that b^2 >= (n - a^2 - b^2)/2
    6. For c in a range (min_c, max_c)
    7. We now look at (n - a^2 - b^2 - c^2). If its square root is an integer, this is d.
    Otherwise, this tuple will not form a solution
    

    At steps 3b and 5b I use (n - a^2)/3, (n - a^2 - b^2)/2. We divide by 3 or 2, respectively, because of the number of values in the tuple not yet 'fixed'.

    An example:

    doing this on n = 12:

    1a. max_a = 3
    1b. min_a = 2
    2. for a in range(2, 3):
        use a = 2
    3a. we now look at (12 - 2^2) = 8
    max_b = 2
    3b. min_b = 2
    4. b must be 2
    5a. we now look at (12 - 2^2 - 2^2) = 4
    max_c = 2
    5b. min_c = 2
    6. c must be 2
    7. (n - a^2 - b^2 - c^2) = 0, hence d = 0
    so a possible tuple is (2, 2, 2, 0)
    
    2. use a = 3
    3a. we now look at (12 - 3^2) = 3
    max_b = 1
    3b. min_b = 1
    4. b must be 1
    5a. we now look at (12 - 3^2 - 1^2) = 2
    max_c = 1
    5b. min_c = 1
    6. c must be 1
    7. (n - a^2 - b^2 - c^2) = 1, hence d = 1
    so a possible tuple is (3, 1, 1, 1)
    

    These are the only two possible tuples - hey presto!

提交回复
热议问题