C++でのオイラーの四平方アイデンティティ
この問題では、2つの数値が与えられ、オイラーの4平方アイデンティティを使用して数値の積を見つける必要があります。
オイラーの四平方アイデンティティ は、4つの正方形の合計を使用して表すことができる2つの数値の積を見つける方法です。 数が4つの正方形の合計として表すことができる場合は数の。
積a*bは、次の場合に4つの二乗の合計として表すことができます
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、
これは複数の方法で表すことができます。ここで1つの解決策を検討します
540 =1 * 1 + 1 * 1 + 3 * 3 + 23 * 23 =1 + 1 + 9 + 529
ソリューションアプローチ-
問題の簡単な解決策は、問題を解決するために4つの正方形の組み合わせのそれぞれを試すための試行方法を使用することです。このために、ネストされたループを使用します。正方形の値ごとに1つのネストされたループです。次に、計算された値を見つけて、合計表現のすべての可能な組み合わせを出力します。
このソリューションは少し複雑であり、時間の複雑さはオーダーです
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<<"The product "<<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 The product 54*10 = 540 represented as 1*1 + 1*1 + 3*3 + 23*23 The product 54*10 = 540 represented as 1*1 + 3*3 + 13*13 + 19*19 The product 54*10 = 540 represented as 1*1 + 5*5 + 15*15 + 17*17 The product 54*10 = 540 represented as 1*1 + 7*7 + 7*7 + 21*21 The product 54*10 = 540 represented as 1*1 + 9*9 + 13*13 + 17*17 The product 54*10 = 540 represented as 2*2 + 4*4 + 6*6 + 22*22 The product 54*10 = 540 represented as 2*2 + 4*4 + 14*14 + 18*18 The product 54*10 = 540 represented as 2*2 + 6*6 + 10*10 + 20*20 The product 54*10 = 540 represented as 2*2 + 12*12 + 14*14 + 14*14 The product 54*10 = 540 represented as 3*3 + 3*3 + 9*9 + 21*21 The product 54*10 = 540 represented as 3*3 + 7*7 + 11*11 + 19*19 The product 54*10 = 540 represented as 3*3 + 9*9 + 15*15 + 15*15 The product 54*10 = 540 represented as 3*3 + 11*11 + 11*11 + 17*17 The product 54*10 = 540 represented as 4*4 + 10*10 + 10*10 + 18*18 The product 54*10 = 540 represented as 5*5 + 5*5 + 7*7 + 21*21 The product 54*10 = 540 represented as 5*5 + 11*11 + 13*13 + 15*15 The product 54*10 = 540 represented as 6*6 + 6*6 + 12*12 + 18*18 The product 54*10 = 540 represented as 7*7 + 7*7 + 9*9 + 19*19 The product 54*10 = 540 represented as 7*7 + 9*9 + 11*11 + 17*17 The product 54*10 = 540 represented as 9*9 + 11*11 + 13*13 + 13*13 The product 54*10 = 540 represented as 10*10 + 10*10 + 12*12 + 14*14
別の 時間計算量を減らして問題を解決する方法は、3つのネストされたループを使用し、4番目の値をチェックすることです。残りの数が完全な平方である場合、ルート値は4番目の数であり、そうでない場合、解決は不可能です。この方法では、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++) { sumSquare = (x*x) + (y*y) + (z*z); float k = sqrt(prod - sumSquare); if ( floor(k) == ceil(k)) { cout<<"The product "<<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 The product 54*10 = 540 represented as 1*1 + 1*1 + 3*3 + 23*23 The product 54*10 = 540 represented as 1*1 + 1*1 + 23*23 + 3*3 The product 54*10 = 540 represented as 1*1 + 3*3 + 13*13 + 19*19 The product 54*10 = 540 represented as 1*1 + 3*3 + 19*19 + 13*13 The product 54*10 = 540 represented as 1*1 + 3*3 + 23*23 + 1*1 The product 54*10 = 540 represented as 1*1 + 5*5 + 15*15 + 17*17 The product 54*10 = 540 represented as 1*1 + 5*5 + 17*17 + 15*15 The product 54*10 = 540 represented as 1*1 + 7*7 + 7*7 + 21*21 The product 54*10 = 540 represented as 1*1 + 7*7 + 21*21 + 7*7 The product 54*10 = 540 represented as 1*1 + 9*9 + 13*13 + 17*17 The product 54*10 = 540 represented as 1*1 + 9*9 + 17*17 + 13*13 The product 54*10 = 540 represented as 1*1 + 13*13 + 17*17 + 9*9 The product 54*10 = 540 represented as 1*1 + 13*13 + 19*19 + 3*3 The product 54*10 = 540 represented as 1*1 + 15*15 + 17*17 + 5*5 The product 54*10 = 540 represented as 2*2 + 4*4 + 6*6 + 22*22 The product 54*10 = 540 represented as 2*2 + 4*4 + 14*14 + 18*18 The product 54*10 = 540 represented as 2*2 + 4*4 + 18*18 + 14*14 The product 54*10 = 540 represented as 2*2 + 4*4 + 22*22 + 6*6 The product 54*10 = 540 represented as 2*2 + 6*6 + 10*10 + 20*20 The product 54*10 = 540 represented as 2*2 + 6*6 + 20*20 + 10*10 The product 54*10 = 540 represented as 2*2 + 6*6 + 22*22 + 4*4 The product 54*10 = 540 represented as 2*2 + 10*10 + 20*20 + 6*6 The product 54*10 = 540 represented as 2*2 + 12*12 + 14*14 + 14*14 The product 54*10 = 540 represented as 2*2 + 14*14 + 14*14 + 12*12 The product 54*10 = 540 represented as 2*2 + 14*14 + 18*18 + 4*4 The product 54*10 = 540 represented as 3*3 + 3*3 + 9*9 + 21*21 The product 54*10 = 540 represented as 3*3 + 3*3 + 21*21 + 9*9 The product 54*10 = 540 represented as 3*3 + 7*7 + 11*11 + 19*19 The product 54*10 = 540 represented as 3*3 + 7*7 + 19*19 + 11*11 The product 54*10 = 540 represented as 3*3 + 9*9 + 15*15 + 15*15 The product 54*10 = 540 represented as 3*3 + 9*9 + 21*21 + 3*3 The product 54*10 = 540 represented as 3*3 + 11*11 + 11*11 + 17*17 The product 54*10 = 540 represented as 3*3 + 11*11 + 17*17 + 11*11 The product 54*10 = 540 represented as 3*3 + 11*11 + 19*19 + 7*7 The product 54*10 = 540 represented as 3*3 + 13*13 + 19*19 + 1*1 The product 54*10 = 540 represented as 3*3 + 15*15 + 15*15 + 9*9 The product 54*10 = 540 represented as 4*4 + 6*6 + 22*22 + 2*2 The product 54*10 = 540 represented as 4*4 + 10*10 + 10*10 + 18*18 The product 54*10 = 540 represented as 4*4 + 10*10 + 18*18 + 10*10 The product 54*10 = 540 represented as 4*4 + 14*14 + 18*18 + 2*2 The product 54*10 = 540 represented as 5*5 + 5*5 + 7*7 + 21*21 The product 54*10 = 540 represented as 5*5 + 5*5 + 21*21 + 7*7 The product 54*10 = 540 represented as 5*5 + 7*7 + 21*21 + 5*5 The product 54*10 = 540 represented as 5*5 + 11*11 + 13*13 + 15*15 The product 54*10 = 540 represented as 5*5 + 11*11 + 15*15 + 13*13 The product 54*10 = 540 represented as 5*5 + 13*13 + 15*15 + 11*11 The product 54*10 = 540 represented as 5*5 + 15*15 + 17*17 + 1*1 The product 54*10 = 540 represented as 6*6 + 6*6 + 12*12 + 18*18 The product 54*10 = 540 represented as 6*6 + 6*6 + 18*18 + 12*12 The product 54*10 = 540 represented as 6*6 + 10*10 + 20*20 + 2*2 The product 54*10 = 540 represented as 6*6 + 12*12 + 18*18 + 6*6 The product 54*10 = 540 represented as 7*7 + 7*7 + 9*9 + 19*19 The product 54*10 = 540 represented as 7*7 + 7*7 + 19*19 + 9*9 The product 54*10 = 540 represented as 7*7 + 7*7 + 21*21 + 1*1 The product 54*10 = 540 represented as 7*7 + 9*9 + 11*11 + 17*17 The product 54*10 = 540 represented as 7*7 + 9*9 + 17*17 + 11*11 The product 54*10 = 540 represented as 7*7 + 9*9 + 19*19 + 7*7 The product 54*10 = 540 represented as 7*7 + 11*11 + 17*17 + 9*9 The product 54*10 = 540 represented as 7*7 + 11*11 + 19*19 + 3*3 The product 54*10 = 540 represented as 9*9 + 11*11 + 13*13 + 13*13 The product 54*10 = 540 represented as 9*9 + 11*11 + 17*17 + 7*7 The product 54*10 = 540 represented as 9*9 + 13*13 + 13*13 + 11*11 The product 54*10 = 540 represented as 9*9 + 13*13 + 17*17 + 1*1 The product 54*10 = 540 represented as 9*9 + 15*15 + 15*15 + 3*3 The product 54*10 = 540 represented as 10*10 + 10*10 + 12*12 + 14*14 The product 54*10 = 540 represented as 10*10 + 10*10 + 14*14 + 12*12 The product 54*10 = 540 represented as 10*10 + 10*10 + 18*18 + 4*4 The product 54*10 = 540 represented as 10*10 + 12*12 + 14*14 + 10*10 The product 54*10 = 540 represented as 11*11 + 11*11 + 17*17 + 3*3 The product 54*10 = 540 represented as 11*11 + 13*13 + 13*13 + 9*9 The product 54*10 = 540 represented as 11*11 + 13*13 + 15*15 + 5*5 The product 54*10 = 540 represented as 12*12 + 14*14 + 14*14 + 2*2>
-
C++の正方形の外接円の面積
この問題では、正方形の辺が与えられたときに、正方形の外接円の面積を計算します。先に進む前に、概念をよりよく理解するために基本的な定義を修正しましょう。 正方形 はすべての辺が等しい四辺形です。 外接円 円はポリゴンのすべての頂点に接しています。 エリア は、任意の2次元図形の範囲を定量的に表したものです。 正方形の外接円の面積を計算します。円と正方形のパラメータの関係を見つける必要があります。 これで、図のように、正方形のすべての頂点が円に接触しています。この図を見ると、正方形の対角線は円の直径に等しいと結論付けることができます。 これを使用して、円の直径と正方形の辺の関係を
-
C++でのAreaOfSquareのプログラム
長方形の辺が与えられ、その辺から正方形の領域を印刷することがタスクです。 正方形は、4つの辺を持ち、それぞれ90度の4つの角度を形成し、すべての辺が同じ形状の2D平面図形です。言い換えれば、正方形は辺が等しい長方形の形であると言えます。 以下に示すのは正方形の表現です- 正方形の面積はSidexSide 例 Input: 6 Output: 36 As the side is 6 so the output is 6*6=36 Input: 12 Output: 144 アルゴリズム START Step 1-> Declare a functio