Counting Pythagorean Triples
InterviewTime limit2sMemory limit1024 MB
Given n, count the primitive and non-primitive Pythagorean triples where n is the hypotenuse and where n is a leg.
- Level
Medium4 of 10
- Topics
- Number theory, Math, Brute force
- Solved
- No attempts yet
Problem
A Pythagorean triple is a set of three positive integers a, b, and c that satisfy .
A Pythagorean triple is a primitive Pythagorean triple (PPT) when a, b, and c have no common factor.
Write a program that takes a positive integer n and counts the following.
- The number of different PPTs where n is the hypotenuse (c)
- The number of non-primitive Pythagorean triples where n is the hypotenuse (c)
- The number of different PPTs where n is one of the legs (a or b)
- The number of non-primitive Pythagorean triples where n is one of the legs (a or b)
The triples a, b, c and b, a, c are the same, so each is counted once. A non-primitive Pythagorean triple is a Pythagorean triple that is not a PPT.
For n = 65, the cases for each item are as follows.
- 33, 56, 65; 63, 16, 65
- 39, 52, 65; 25, 60, 65
- 65, 72, 97; 65, 2112, 2113
- 65, 420, 425; 65, 156, 169
Input
The input is a single line containing one non-negative decimal integer n (3 ≤ n ≤ 2500).
Output
The output is one line containing four decimal integers.
The first is the number of different PPTs where n is the hypotenuse (c).
The second is the number of non-primitive Pythagorean triples where n is the hypotenuse (c).
The third is the number of different PPTs where n is one of the legs (a or b).
The fourth is the number of non-primitive Pythagorean triples where n is one of the legs (a or b).