C ++中的欧拉四方形身份

在这个问题上,我们给了两个数字,我们需要使用欧拉的 四个平方恒等式找到这些数字的乘积。 

欧拉的四方形同一性 是一种方法,用于查找两个数字的乘积,如果可以将数字表示为四个平方的和,则可以使用该数字的四个平方 的和来表示。

如果乘积a * b可以表示为四个平方的和

a = x1 2 + x2 2 + x3 2 + x4 2
 b = y1 2 + y2 2 + y3 2 + y4 2
 a * b = z1 2 + z2 2 + z3 2 + z4 2

让我们举个例子来了解这个问题,

输入: 

          a = 54 = 2 * 2 + 3 * 3 + 4 * 4 + 5 * 5

          b = 10 = 1 * 1 + 2 * 2 + 1 * 1 + 2 * 2

输出:  1 * 1 + 1 * 1 + 3 * 3 + 23 * 23

解释: 

a和b的乘积= 540,

可以用多种方式表示。我们会在这里找到一种解决方案,

540 = 1 * 1 + 1 * 1 + 3 * 3 + 23 * 23 = 1 +1 + 9 + 529

解决方法-

解决该问题的简单方法是使用试验方法尝试四种方形组合中的每一种来解决该问题。为此,我们将使用嵌套循环,每个平方值一个嵌套循环。然后找到计算出的值,并打印所有可能的和表示形式的组合。

该解决方案有点复杂,时间复杂度约为
O((a * b)4)。 

该程序说明了我们解决方案的工作原理,

示例

#include <bits/stdc++.h>
using namespace std;

void findEulerSquareNumberValue(int a, int b) {

   int prod = a * b;
   int sumSquare = 0;
   for (int x = 0; x <= sqrt(prod); x++) {
      for (int y = x; y <= sqrt(prod); y++) {
         for (int z = y; z <= sqrt(prod); z++) {
            for (int k = z; k <= sqrt(prod); k++) {
           
               sumSquare = (x*x) + (y*y) + (z*z) + (k*k);
               if (sumSquare == prod) {
                cout<<"产品 "<<a<<"*"<<b<<" = "<<prod<<" represented as ";
                cout<<x<<"*"<<x<<" + ";
                cout<<y<<"*"<<y<<" + ";
                cout<<z<<"*"<<z<<" + ";
                cout<<k<<"*"<<k<<endl;
                cout<<endl;
               }
            }
         }
      }
   }
}

int main() {

   int a = (2*2) + (3*3) + (4*4) + (5*5);
   int b = (1*1) + (2*2) + (1*1) + (2*2);
   cout<<"a = (2*2) + (3*3) + (4*4) + (5*5) = "<<a<<endl;
   cout<<"b = (1*1) + (2*2) + (1*1) + (2*2) = "<<b<<endl;
   findEulerSquareNumberValue(a, b);
   
   return 0;
}

输出-

a = (2*2) + (3*3) + (4*4) + (5*5) = 54
b = (1*1) + (2*2) + (1*1) + (2*2) = 10
产品 54*10 = 540 represented as 1*1 + 1*1 + 3*3 + 23*23

产品 54*10 = 540 represented as 1*1 + 3*3 + 13*13 + 19*19

产品 54*10 = 540 represented as 1*1 + 5*5 + 15*15 + 17*17

产品 54*10 = 540 represented as 1*1 + 7*7 + 7*7 + 21*21

产品 54*10 = 540 represented as 1*1 + 9*9 + 13*13 + 17*17

产品 54*10 = 540 represented as 2*2 + 4*4 + 6*6 + 22*22

产品 54*10 = 540 represented as 2*2 + 4*4 + 14*14 + 18*18

产品 54*10 = 540 represented as 2*2 + 6*6 + 10*10 + 20*20

产品 54*10 = 540 represented as 2*2 + 12*12 + 14*14 + 14*14

产品 54*10 = 540 represented as 3*3 + 3*3 + 9*9 + 21*21

产品 54*10 = 540 represented as 3*3 + 7*7 + 11*11 + 19*19

产品 54*10 = 540 represented as 3*3 + 9*9 + 15*15 + 15*15

产品 54*10 = 540 represented as 3*3 + 11*11 + 11*11 + 17*17

产品 54*10 = 540 represented as 4*4 + 10*10 + 10*10 + 18*18

产品 54*10 = 540 represented as 5*5 + 5*5 + 7*7 + 21*21

产品 54*10 = 540 represented as 5*5 + 11*11 + 13*13 + 15*15

产品 54*10 = 540 represented as 6*6 + 6*6 + 12*12 + 18*18

产品 54*10 = 540 represented as 7*7 + 7*7 + 9*9 + 19*19

产品 54*10 = 540 represented as 7*7 + 9*9 + 11*11 + 17*17

产品 54*10 = 540 represented as 9*9 + 11*11 + 13*13 + 13*13

产品 54*10 = 540 represented as 10*10 + 10*10 + 12*12 + 14*14

解决时间复杂度较低问题的另一种 方法是使用三个嵌套循环并检查第四个值,如果剩余数为正平方,则根值为第四个数,否则无法解决。此方法可能会消除第四个循环并使解决方案有效。

该程序说明了我们解决方案的工作原理,

示例

#include <bits/stdc++.h>
using namespace std;

void findEulerSquareNumberValue(int a, int b) {

   int prod = a * b;
   int sumSquare = 0;
   for (int x = 0; x <= sqrt(prod); x++) {
      for (int y = x; y <= sqrt(prod); y++) {
         for (int z = y; z <= sqrt(prod); z++) {
         
            sumSquare = (x*x) + (y*y) + (z*z);
            float k = sqrt(prod - sumSquare);
            if ( floor(k) == ceil(k)) {
             cout<<"产品 "<<a<<"*"<<b<<" = "<<prod<<" represented as ";
             cout<<x<<"*"<<x<<" + ";
             cout<<y<<"*"<<y<<" + ";
             cout<<z<<"*"<<z<<" + ";
             cout<<k<<"*"<<k<<endl;
             cout<<endl;
            }
         }
      }
   }
}

int main() {

   int a = (2*2) + (3*3) + (4*4) + (5*5);
   int b = (1*1) + (2*2) + (1*1) + (2*2);
   cout<<"a = (2*2) + (3*3) + (4*4) + (5*5) = "<<a<<endl;
   cout<<"b = (1*1) + (2*2) + (1*1) + (2*2) = "<<b<<endl;
   findEulerSquareNumberValue(a, b);
   
   return 0;
}

输出-

a = (2*2) + (3*3) + (4*4) + (5*5) = 54
b = (1*1) + (2*2) + (1*1) + (2*2) = 10
产品 54*10 = 540 represented as 1*1 + 1*1 + 3*3 + 23*23

产品 54*10 = 540 represented as 1*1 + 1*1 + 23*23 + 3*3

产品 54*10 = 540 represented as 1*1 + 3*3 + 13*13 + 19*19

产品 54*10 = 540 represented as 1*1 + 3*3 + 19*19 + 13*13

产品 54*10 = 540 represented as 1*1 + 3*3 + 23*23 + 1*1

产品 54*10 = 540 represented as 1*1 + 5*5 + 15*15 + 17*17

产品 54*10 = 540 represented as 1*1 + 5*5 + 17*17 + 15*15

产品 54*10 = 540 represented as 1*1 + 7*7 + 7*7 + 21*21

产品 54*10 = 540 represented as 1*1 + 7*7 + 21*21 + 7*7

产品 54*10 = 540 represented as 1*1 + 9*9 + 13*13 + 17*17

产品 54*10 = 540 represented as 1*1 + 9*9 + 17*17 + 13*13

产品 54*10 = 540 represented as 1*1 + 13*13 + 17*17 + 9*9

产品 54*10 = 540 represented as 1*1 + 13*13 + 19*19 + 3*3

产品 54*10 = 540 represented as 1*1 + 15*15 + 17*17 + 5*5

产品 54*10 = 540 represented as 2*2 + 4*4 + 6*6 + 22*22

产品 54*10 = 540 represented as 2*2 + 4*4 + 14*14 + 18*18

产品 54*10 = 540 represented as 2*2 + 4*4 + 18*18 + 14*14

产品 54*10 = 540 represented as 2*2 + 4*4 + 22*22 + 6*6

产品 54*10 = 540 represented as 2*2 + 6*6 + 10*10 + 20*20

产品 54*10 = 540 represented as 2*2 + 6*6 + 20*20 + 10*10

产品 54*10 = 540 represented as 2*2 + 6*6 + 22*22 + 4*4

产品 54*10 = 540 represented as 2*2 + 10*10 + 20*20 + 6*6

产品 54*10 = 540 represented as 2*2 + 12*12 + 14*14 + 14*14

产品 54*10 = 540 represented as 2*2 + 14*14 + 14*14 + 12*12

产品 54*10 = 540 represented as 2*2 + 14*14 + 18*18 + 4*4

产品 54*10 = 540 represented as 3*3 + 3*3 + 9*9 + 21*21

产品 54*10 = 540 represented as 3*3 + 3*3 + 21*21 + 9*9

产品 54*10 = 540 represented as 3*3 + 7*7 + 11*11 + 19*19

产品 54*10 = 540 represented as 3*3 + 7*7 + 19*19 + 11*11

产品 54*10 = 540 represented as 3*3 + 9*9 + 15*15 + 15*15

产品 54*10 = 540 represented as 3*3 + 9*9 + 21*21 + 3*3

产品 54*10 = 540 represented as 3*3 + 11*11 + 11*11 + 17*17

产品 54*10 = 540 represented as 3*3 + 11*11 + 17*17 + 11*11

产品 54*10 = 540 represented as 3*3 + 11*11 + 19*19 + 7*7

产品 54*10 = 540 represented as 3*3 + 13*13 + 19*19 + 1*1

产品 54*10 = 540 represented as 3*3 + 15*15 + 15*15 + 9*9

产品 54*10 = 540 represented as 4*4 + 6*6 + 22*22 + 2*2

产品 54*10 = 540 represented as 4*4 + 10*10 + 10*10 + 18*18

产品 54*10 = 540 represented as 4*4 + 10*10 + 18*18 + 10*10

产品 54*10 = 540 represented as 4*4 + 14*14 + 18*18 + 2*2

产品 54*10 = 540 represented as 5*5 + 5*5 + 7*7 + 21*21

产品 54*10 = 540 represented as 5*5 + 5*5 + 21*21 + 7*7

产品 54*10 = 540 represented as 5*5 + 7*7 + 21*21 + 5*5

产品 54*10 = 540 represented as 5*5 + 11*11 + 13*13 + 15*15

产品 54*10 = 540 represented as 5*5 + 11*11 + 15*15 + 13*13

产品 54*10 = 540 represented as 5*5 + 13*13 + 15*15 + 11*11

产品 54*10 = 540 represented as 5*5 + 15*15 + 17*17 + 1*1

产品 54*10 = 540 represented as 6*6 + 6*6 + 12*12 + 18*18

产品 54*10 = 540 represented as 6*6 + 6*6 + 18*18 + 12*12

产品 54*10 = 540 represented as 6*6 + 10*10 + 20*20 + 2*2

产品 54*10 = 540 represented as 6*6 + 12*12 + 18*18 + 6*6

产品 54*10 = 540 represented as 7*7 + 7*7 + 9*9 + 19*19

产品 54*10 = 540 represented as 7*7 + 7*7 + 19*19 + 9*9

产品 54*10 = 540 represented as 7*7 + 7*7 + 21*21 + 1*1

产品 54*10 = 540 represented as 7*7 + 9*9 + 11*11 + 17*17

产品 54*10 = 540 represented as 7*7 + 9*9 + 17*17 + 11*11

产品 54*10 = 540 represented as 7*7 + 9*9 + 19*19 + 7*7

产品 54*10 = 540 represented as 7*7 + 11*11 + 17*17 + 9*9

产品 54*10 = 540 represented as 7*7 + 11*11 + 19*19 + 3*3

产品 54*10 = 540 represented as 9*9 + 11*11 + 13*13 + 13*13

产品 54*10 = 540 represented as 9*9 + 11*11 + 17*17 + 7*7

产品 54*10 = 540 represented as 9*9 + 13*13 + 13*13 + 11*11

产品 54*10 = 540 represented as 9*9 + 13*13 + 17*17 + 1*1

产品 54*10 = 540 represented as 9*9 + 15*15 + 15*15 + 3*3

产品 54*10 = 540 represented as 10*10 + 10*10 + 12*12 + 14*14

产品 54*10 = 540 represented as 10*10 + 10*10 + 14*14 + 12*12

产品 54*10 = 540 represented as 10*10 + 10*10 + 18*18 + 4*4

产品 54*10 = 540 represented as 10*10 + 12*12 + 14*14 + 10*10

产品 54*10 = 540 represented as 11*11 + 11*11 + 17*17 + 3*3

产品 54*10 = 540 represented as 11*11 + 13*13 + 13*13 + 9*9

产品 54*10 = 540 represented as 11*11 + 13*13 + 15*15 + 5*5

产品 54*10 = 540 represented as 12*12 + 14*14 + 14*14 + 2*2