- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Pythagorean Quadruple
A group of four positive integers (a, b, c, and d) that satisfy the Pythagorean equation are called Pythagorean quadruples.
The equation can be written as: a2 + b2 + c2 = d2 , with ‘d’ being the largest value out of the given numbers. In other words, the square of the fourth integer should be equal to the sum obtained by adding the squares of the previous three numbers.
(1, 2, 2, 3) is a Pythagorean quadruplet as (12 + 22 + 22) = (1 + 4 + 4) = (9) = (32). Due to the requirement that they satisfy the Pythagorean equation, not all sets of four positive integers can be Pythagorean quadruples. Pythagorean quadruples can be used to create intriguing mathematical patterns and have applications in number theory as well.
Problem Statement
The aim of this problem is to check whether the 4 given input integers form a Pythagorean Quadruple.
Examples
Input: {2, 3, 6, 7} Output: True
Explanation −
The sum of the squares of the first three numbers can be written as,
(22 + 32 + 62) = (4 + 9 + 36) = 49
The square of the fourth number is 72 = 49, which is equal to the previous result. Hence the numbers, {2, 3, 6, 7}, form a Pythagorean Quadruplet so we return True.
Input: {1, 4, 8, 9} Output: True
Explanation −
The sum of the squares of the first three numbers can be written as,
(12 + 42 + 82) = (1 + 16 + 64) = 81
The square of the fourth number is 92 = 81, which is equal to the previous result. Hence the numbers, {1, 4, 8, 9}, form a Pythagorean Quadruplet so we return True.
Input: {1, 2, 3, 4} Output: False
Explanation −
The sum of the squares of the first three numbers can be written as,
(12 + 22 + 32) = (1 + 4 + 9) = 14
The square of the fourth number is 42 = 16, which is not equal to the previous result. Hence the numbers, {1, 4, 8, 9}, do not form a Pythagorean Quadruplet so we return False.
Solution Approach
In order to determine whether the 4 given numbers form a Pythagorean Quadruplet, we must figure out if the summation of the squares of the first 3 numbers is equal to the square of the fourth number, where the fourth number has the largest magnitude.
Algorithm
The approach consists of the following steps −
Sort the vector of numbers in non-decreasing order.
Compute the sum of squares of the first three numbers.
Compute the square of the fourth number.
Check if the results obtained from step 2 and step 3 are equal.
Display the answer.
Pseudo Code
Function is_pythagorean_quadruplet()
Initialise squareSum.
squareSum = a2 + b2 + c2
Return (squareSum == d2)
Function main()
Initialise vector<int>nums = {a, b, c, d}
sort (nums.begin(), nums.end())
if (is_pythagorean_quadruplet())
return true
else
return false
Print output
Example: C++ Program
This program code checks if the 4 given numbers, stored in a vector, form a pythagorean quadruplet using the is_pythagorean_quadruplet() boolean function. It initially sorts the vector using the C++ standard template library sort() function. It then computes the sum of squares of the first three numbers and stores them in the squareSum variable. The function is_pythagorean_quadruplet() returns true if the value of squareSum is equal to the square of the fourth number else it returns false.
// C++ code for to check if the 4 given numbers form a Pythagorean Quadruple #include <iostream> #include <cmath> #include <vector> #include <algorithm> using namespace std; // this function is used to check out whether the 4 given numbers form a Pythagorean Quadruplet or not bool is_pythagorean_quadruplet(vector<int> nums){ int squareSum; squareSum = pow(nums[0], 2) + pow(nums[1], 2) + pow(nums[2], 2); // 9 return (squareSum == pow(nums[3], 2)); // compares summation of square of first three numbers with square of fourth number (9 == 9) } int main(){ vector<int> nums = {1, 2, 2, 3}; sort(nums.begin(), nums.end()); // sorts the numbers in non-decreasing order if (is_pythagorean_quadruplet(nums)) { cout << "True" << endl; } else { cout << "False" << endl; } return 0; }
Output
True
Time and Space Complexity Analysis
Time Complexity: O(1)
O(1), since it does a fixed amount of operations. The squareSum is calculated using three constant-time operations by the function is_pythagorean_quadruplet(), and its equality with the fourth element raised to the power of two is tested.
In the worst scenario, the sort function's time complexity is O(nlog(n)). As a result, the overall time complexity of the code should be O(nlog(n)), where n is the number of elements in the vector. Yet, the time complexity of the sort function remains constant because the size of the vector is set at 4 in each case
Space Complexity: O(1)
This is because the space required to store the input vector is always 4. No auxiliary space is needed in the implementation of the code; therefore the space complexity can be considered constant.
Conclusion
In conclusion, a Pythagorean Quadruple is a tuple of integers a, b, c, and d such that
a2 + b2 + c2 = d2. The algorithm presented in this article provides a straightforward and efficient way to determine if a tuple of four integers forms a Pythagorean quadruplet in constant time without the use of auxiliary space. The article discusses the basic concept of Pythagorean Quadruples along with suitable examples. It also provides the solution approach, the algorithm used and the C++ program solution as well, along with an in depth analysis of its time complexity and space complexity.