Under construction

Still being built. Things may change

Counting Pythagorean Triples

Interview

Time limit2sMemory limit1024 MB

Summary
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 a2+b2=c2a^2 + b^2 = c^2.

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.

  1. The number of different PPTs where n is the hypotenuse (c)
  2. The number of non-primitive Pythagorean triples where n is the hypotenuse (c)
  3. The number of different PPTs where n is one of the legs (a or b)
  4. 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.

  1. 33, 56, 65; 63, 16, 65
  2. 39, 52, 65; 25, 60, 65
  3. 65, 72, 97; 65, 2112, 2113
  4. 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).

Examples3

  1. Example 1

    Input
    65
    
    Expected output
    2 2 2 2
    
  2. Example 2

    Input
    64
    
    Expected output
    0 0 1 4
    
  3. Example 3

    Input
    2023
    
    Expected output
    0 2 2 5