Given four points, check whether they form Pythagorean Quadruple.
It is defined as a tuple of integers a, b, c, d such that a^2 + b^2 + c^2 = d^2 . They are basically the solutions of Diophantine Equations. In the geometric interpretation it represents a cuboid with integer side lengths |a|, |b|, |c| and whose space diagonal is |d| .

The cuboids sides shown here are examples of pythagorean quadruples.
It is primitive when their greatest common divisor is 1. Every Pythagorean quadruple is an integer multiple of a primitive quadruple. We can generate the set of primitive pythagorean quadruples for which a is odd can be generated by formula :
a = m2 + n2 - p2 - q2,
b = 2(mq + np),
c = 2(nq - mp),
d = m2 + n2 + p2 + q2
where m, n, p, q are non-negative integers with greatest common divisor 1 such that m + n + p + q are odd. Thus, all primitive Pythagorean quadruples are characterized by Lebesgue's identity.
(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 <bits/stdc++.h>
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
<?php
// php code to detect Pythagorean Quadruples.
// function for checking
function 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))
echo "Yes" ;
else
echo "No" ;
// This code is contributed by anuj_67.
?>
JavaScript
<script>
// JavaScript program to detect Pythagorean Quadruples.
// function for checking
function pythagorean_quadruple(a, b,
c, d)
{
let sum = a * a + b * b + c * c;
if (d * d == sum)
return true;
else
return false;
}
// Driver code
let a = 1, b = 2, c = 2, d = 3;
if (pythagorean_quadruple(a, b, c, d))
document.write("Yes");
else
document.write("No" );
</script>
Output:
Yes
Time Complexity: O(1)
Auxiliary Space: O(1)
References
Wiki
mathworld
Similar Reads
Generate Pythagorean Triplets Given a positive integer limit, your task is to find all possible Pythagorean Triplet (a, b, c), such that a <= b <= c <= limit.Note: A Pythagorean triplet is a set of three positive integers a, b, and c such that a2 + b2 = c2. Input: limit = 20Output: 3 4 5 5 12 13 6 8 10 8 15 17 9 12 15 1
14 min read
3 Sum - Pythagorean Triplet in an array Given an array of positive integers, the task is to determine if a Pythagorean triplet exists in the given array. A triplet {a, b, c} is considered a Pythagorean triplet if it satisfies the condition a2 + b2 = c2.Example:Input: arr[] = [3, 1, 4, 6, 5] Output: trueExplanation: The array contains a Py
15+ min read
Find Pythagorean Triplet with given sum Given a positive integer target, the task is to find all Pythagorean Triplets whose sum of the elements is equal to the given target. A triplet {a, b, c} is considered a Pythagorean triplet if it satisfies the condition a2 + b2 = c2.Examples : Input: target = 60Output: {{10, 24, 26}, {15, 20, 25}}Ex
11 min read
Twin Pythagorean triplets in an array Given an array of integers arr[], the task is to check if there is a Twin Pythagorean Triplet in the array.A Twin Pythagorean Triplet is a Pythagorean triplet (a, b, c) for which two values are consecutive integers. The first few twin triples are (3, 4, 5), (5, 12, 13), (7, 24, 25), (20, 21, 29), (9
13 min read
POTD Solutions | 3 Novâ 23 | Pythagorean Triplet View all POTD Solutions Welcome to the daily solutions of our PROBLEM OF THE DAY (POTD). We will discuss the entire problem step-by-step and work towards developing an optimized solution. This will not only help you brush up on your concepts of Hashing but will also help you build up problem-solving
8 min read