Pythagorean Quadruple
February 25, 2020
A Pythagorean quadruple consists of four positive integers a, b, c and d such that a ≤ b ≤ c ≤ d and a² + b² + c² = d². For instance, (2 3 6 7) is a Pythagorean quadruple because 2² + 3² + 6² = 4 + 9 + 36 = 49 = 7².
Your task is to write a program that counts the Pythagorian quadruples with a, b, c less than or equal to some given N, and compute the number of Pythagorian quadruples with N = 1000. When you are finished, you are welcome to read or run a suggested solution, or to post your own solution or discuss the exercise in the comments below.
Interesting problem. Here is my take on it using Julia: https://pastebin.com/eYYR51sm
For N = 1000 I found 85490 quadruples (allowing for multiple instances of the same number in the quadruple set).
Cheers
Here’s a C++ solution that runs in O(n²) time, using O(n²) space (assuming hash table access time is constant):
Just in case overflow in all those squares is a problem, I thought I’d see what gcc can do to check things at runtime:
Here’s a slightly improved version (that prints spaces between the values for a,b,c,d):
Here’s a solution in Python.
Output: