给定四点,检查它们是否形成勾股四边形。
它被定义为整数 a, b, c, d 的元组,使得 .它们基本上是丢番图方程的解。在几何解释中,它表示一个边长为整数的长方体|a|, |b|, |c|其空间对角线为|d| .
此处显示的长方体边是勾股四边形的示例。
当它们的最大公约数为 1 时,它是原始的。每个勾股四元组都是原始四元组的整数倍。我们可以生成一组原始毕达哥拉斯四元组,其中 a 是奇数可以通过以下公式生成:
a = m2 + n2 – p2 – q2,
b = 2(mq + np),
c = 2(nq – mp),
d = m2 + n2 + p2 + q2
其中 m, n, p, q 是最大公约数为 1 的非负整数,使得 m + n + p + q 是奇数。因此,所有原始毕达哥拉斯四元组都以勒贝格恒等式为特征。
(m2 + n2 + p2 + q2)2 = (2mq + 2nq)2 + 2(nq – mp)2 + (m2 + n2 – p2 – q2)m2 + n2 – p2 – q2
C++
// C++ code to detect Pythagorean Quadruples.
#include
using namespace std;
// function for checking
bool pythagorean_quadruple(int a, int b, int c,
int d)
{
int sum = a * a + b * b + c * c;
if (d * d == sum)
return true;
else
return false;
}
// Driver Code
int main()
{
int a = 1, b = 2, c = 2, d = 3;
if (pythagorean_quadruple(a, b, c, d))
cout << "Yes" << endl;
else
cout << "No" << endl;
}
Java
// Java code to detect Pythagorean Quadruples.
import java.io.*;
import java.util.*;
class GFG {
// function for checking
static Boolean pythagorean_quadruple(int a, int b,
int c, int d)
{
int sum = a * a + b * b + c * c;
if (d * d == sum)
return true;
else
return false;
}
// Driver function
public static void main (String[] args) {
int a = 1, b = 2, c = 2, d = 3;
if (pythagorean_quadruple(a, b, c, d))
System.out.println("Yes");
else
System.out.println("No" );
}
}
// This code is contributed by Gitanjali.
Python3
# Python code to detect
# Pythagorean Quadruples.
import math
# function for checking
def pythagorean_quadruple(a,b, c, d):
sum = a * a + b * b + c * c;
if (d * d == sum):
return True
else:
return False
#driver code
a = 1
b = 2
c = 2
d = 3
if (pythagorean_quadruple(a, b, c, d)):
print("Yes")
else:
print("No" )
# This code is contributed
# by Gitanjali.
C#
// C# code to detect
// Pythagorean Quadruples.
using System;
class GFG {
// function for checking
static Boolean pythagorean_quadruple(int a,
int b, int c, int d)
{
int sum = a * a + b * b + c * c;
if (d * d == sum)
return true;
else
return false;
}
// Driver function
public static void Main () {
int a = 1, b = 2, c = 2, d = 3;
if (pythagorean_quadruple(a, b, c, d))
Console.WriteLine("Yes");
else
Console.WriteLine("No" );
}
}
// This code is contributed by vt_M.
PHP
Javascript
输出:
Yes
参考
维基
数学世界
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。