The Wayback Machine - https://web.archive.org/web/20241127194355/https://www.geeksforgeeks.org/pythagorean-quadruple/
Open In App

Pythagorean Quadruple

Last Updated : 22 Jun, 2022
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

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
 



Previous Article
Next Article

Similar Reads

Maximum product quadruple (sub-sequence of size 4) in array
Given an integer array, find a maximum product of a quadruple in the array. Examples: Input: [10, 3, 5, 6, 20] Output: 6000 Multiplication of 10, 5, 6 and 20 Input: [-10, -3, -5, -6, -20] Output: 6000 Input: [1, -4, 3, -6, 7, 0] Output: 504 Approach 1 (Naive, O([Tex] [/Tex]) time, O(1) Space) Steps to solve this problem: 1.check if n is smaller tha
15+ min read
4 Sum - Check if a Quadruple with given Sum Exists in an Array
Given an array of integers, check if there are four elements in the array with given sum. Example: Input: arr = {10, 20, 30, 40, 1, 2}, target = 91 Output: TrueExplanation: Sum of 20 + 30 + 40 + 1 = 91 Input: arr = {1, 2, 3, 4, 5, 9, 7, 8}, target = 16 Output: TrueExplanation: Sum of output is equal to 16, i.e. 1 + 3 + 5 + 7 = 16. Input: arr = {1,
14 min read
Check if a number is a Pythagorean Prime or not
Given a positive integer N, check if it is Pythagorean prime or not. If it is a Pythagorean prime, print 'Yes' otherwise print 'No'.Pythagorean primes : A prime number of the form 4*n + 1 is a Pythagorean prime. It can also be expressed as sum of two squares. Pythagorean primes in the range 1 - 100 are: 5, 13, 17, 29, 37, 41, 53, 61, 73, 89, 97 Exa
6 min read
Possible pairs forming a Pythagorean Triple with a given value
Given an integer C, the task is to find all possible pairs (A, B) in range [1, C) such that: A2 + B2 = C2A < B Examples: Input: C = 5 Output:(3, 4) Explanation: (3)2 + (4)2 = 9 + 16 = 25 = 52 Input: C = 25 Output:(15, 20), (7, 24) Explanation: Both the pairs satisfy the necessary conditions. Approach: Check all possible values of A and B in the
6 min read
Generate Pythagorean Triplets
A Pythagorean triplet is a set of three positive integers a, b and c such that a2 + b2 = c2. Given a limit, generate all Pythagorean Triples with values smaller than given limit. Input : limit = 20 Output : 3 4 5 8 6 10 5 12 13 15 8 17 12 16 20 A Simple Solution is to generate these triplets smaller than given limit using three nested loop. For eve
7 min read
Check if a Lexicographical Pythagorean Triplets exists in range [0, K) of lexicographically largest string
Given a string str and a positive integer K. The task is to find if there exist a Pythagorean Triples in the first window of size K of a string which have the same characters as str but is largest in lexicographical order. Note: Every character is in lower case and consider the following values for each alphabet to check if there exist Pythagorean
8 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, 40, 41), (11, 60, 61), (13, 84, 85), (15, 112,
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 skills. POTD 3 November: Pythagorean TripletGiven
8 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 Pythagorean triplet {3, 4, 5}. Input: arr[] = {10,
15+ min read
Find Pythagorean Triplet with given sum using Mathematical Formula
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}}Explanation: There are two Pythagorean triplets: {10
8 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}}Explanation: There are two Pythagorean triplets: {10
11 min read
Linked List Data Structure
A linked list is a fundamental data structure in computer science. It mainly allows efficient insertion and deletion operations compared to arrays. Like arrays, it is also used to implement other data structures like stack, queue and deque. Here’s the comparison of Linked List vs Arrays Linked List: Data Structure: Non-contiguous Memory Allocation:
3 min read
Binary Search Tree
A Binary Search Tree (or BST) is a data structure used in computer science for organizing and storing data in a sorted manner. Each node in a Binary Search Tree has at most two children, a left child and a right child, with the left child containing values less than the parent node and the right child containing values greater than the parent node.
3 min read
Sorting Algorithms
A Sorting Algorithm is used to rearrange a given array or list of elements in an order. Sorting is provided in library implementation of most of the programming languages. Basics of Sorting Algorithms:Introduction to Sorting Applications of Sorting Sorting Algorithms:Comparison Based : Selection Sort, Bubble Sort, Insertion Sort, Merge Sort, Quick
3 min read
Data Structures Tutorial
Data structures are the fundamental building blocks of computer programming. They define how data is organized, stored, and manipulated within a program. Understanding data structures is very important for developing efficient and effective algorithms. In this tutorial, we will explore the most commonly used data structures, including arrays, linke
5 min read
DSA Tutorial - Learn Data Structures and Algorithms
Data Structures and Algorithms (DSA) refer to the study of data structures used for organizing and storing data, along with the design of algorithms for solving problems that operate on these data structures. Why to Learn DSA?Whether it’s finding the shortest path in a GPS system or optimizing search results in a search engine, DSA plays an importa
9 min read
AVL Tree Data Structure
An AVL tree defined as a self-balancing Binary Search Tree (BST) where the difference between heights of left and right subtrees for any node cannot be more than one. The difference between the heights of the left subtree and the right subtree for any node is known as the balance factor of the node. The AVL tree is named after its inventors, Georgy
3 min read
Bubble Sort Algorithm
Bubble Sort is the simplest sorting algorithm that works by repeatedly swapping the adjacent elements if they are in the wrong order. This algorithm is not suitable for large data sets as its average and worst-case time complexity are quite high. We sort the array using multiple passes. After the first pass, the maximum element goes to end (its cor
8 min read
Selection Sort
Selection Sort is a comparison-based sorting algorithm. It sorts an array by repeatedly selecting the smallest (or largest) element from the unsorted portion and swapping it with the first unsorted element. This process continues until the entire array is sorted. First we find the smallest element and swap it with the first element. This way we get
8 min read
Binary Search Algorithm - Iterative and Recursive Implementation
Binary Search Algorithm is a searching algorithm used in a sorted array by repeatedly dividing the search interval in half. The idea of binary search is to use the information that the array is sorted and reduce the time complexity to O(log N). Table of Content What is Binary Search Algorithm?Conditions to apply Binary Search Algorithm in a Data St
15+ min read
Quick Sort
QuickSort is a sorting algorithm based on the Divide and Conquer that picks an element as a pivot and partitions the given array around the picked pivot by placing the pivot in its correct position in the sorted array. Table of Content How does QuickSort Algorithm work? Working of Partition Algorithm with IllustrationIllustration of QuickSort Algor
13 min read
Heap Sort - Data Structures and Algorithms Tutorials
Heap sort is a comparison-based sorting technique based on Binary Heap Data Structure. It can be seen as an optimization over selection sort where we first find the max (or min) element and swap it with the last (or first). We repeat the same process for the remaining elements. In Heap Sort, we use Binary Heap so that we can quickly find and move t
15 min read
Merge Sort - Data Structure and Algorithms Tutorials
Merge sort is a sorting algorithm that follows the divide-and-conquer approach. It works by recursively dividing the input array into smaller subarrays and sorting those subarrays then merging them back together to obtain the sorted array. In simple terms, we can say that the process of merge sort is to divide the array into two halves, sort each h
15 min read
Insertion Sort Algorithm
Insertion sort is a simple sorting algorithm that works by iteratively inserting each element of an unsorted list into its correct position in a sorted portion of the list. It is like sorting playing cards in your hands. You split the cards into two groups: the sorted cards and the unsorted cards. Then, you pick a card from the unsorted group and p
10 min read
How to find Shortest Paths from Source to all Vertices using Dijkstra's Algorithm
Given a weighted graph and a source vertex in the graph, find the shortest paths from the source to all the other vertices in the given graph. Note: The given graph does not contain any negative edge. Examples: Input: src = 0, the graph is shown below. Output: 0 4 12 19 21 11 9 8 14 Explanation: The distance from 0 to 1 = 4. The minimum distance fr
15+ min read
Prim’s Algorithm for Minimum Spanning Tree (MST)
Introduction to Prim's algorithm:We have discussed Kruskal's algorithm for Minimum Spanning Tree. Like Kruskal's algorithm, Prim’s algorithm is also a Greedy algorithm. This algorithm always starts with a single node and moves through several adjacent nodes, in order to explore all of the connected edges along the way. The algorithm starts with an
15+ min read
Kruskal’s Minimum Spanning Tree (MST) Algorithm
A minimum spanning tree (MST) or minimum weight spanning tree for a weighted, connected, undirected graph is a spanning tree with a weight less than or equal to the weight of every other spanning tree. To learn more about Minimum Spanning Tree, refer to this article. Introduction to Kruskal's Algorithm:Here we will discuss Kruskal's algorithm to fi
15+ min read
Breadth First Search or BFS for a Graph
Breadth First Search (BFS) is a fundamental graph traversal algorithm. It begins with a node, then first traverses all its adjacent. Once all adjacent are visited, then their adjacent are traversed. This is different from DFS in a way that closest vertices are visited before others. We mainly traverse vertices level by level. A lot of popular graph
15+ min read
Depth First Search or DFS for a Graph
Depth First Traversal (or DFS) for a graph is similar to Depth First Traversal of a tree. Like trees, we traverse all adjacent vertices one by one. When we traverse an adjacent vertex, we completely finish the traversal of all vertices reachable through that adjacent vertex. After we finish traversing one adjacent vertex and its reachable vertices,
15+ min read
Tree Traversal Techniques
Tree Traversal techniques include various ways to visit all the nodes of the tree. Unlike linear data structures (Array, Linked List, Queues, Stacks, etc) which have only one logical way to traverse them, trees can be traversed in different ways. In this article, we will discuss about all the tree traversal techniques along with their uses. Table o
15+ min read
Article Tags :
Practice Tags :
three90RightbarBannerImg