Show problems as
Classic problem #1925EasyMathEnumeration

Count Square Sum Triples

Real-world version: Triples of Squares That Add Up
Tracked in this browser only
Read Count Square Sum Triples on LeetCode(opens in a new tab)

The statement here is our own: the same inputs, outputs and technique.

Return how many ordered triples (a, b, c) of whole numbers between 1 and n satisfy a * a + b * b == c * c.

Triples in a different order count separately, so (3, 4, 5) and (4, 3, 5) are two of them.

Examples

Example 1

Input
n = 13
Output
6

Three pairs of legs work within thirteen, each counted twice for its two orders: 3 with 4, 6 with 8, and 5 with 12.

Example 2

Input
n = 12
Output
4

Two pairs work within twelve, 3 with 4 and 6 with 8, each counted twice.

Example 3

Input
n = 4
Output
0

Nothing within four works at all.

Constraints

  • 1 <= n <= 250

The signature

The editor starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.

Python
def count_triples(n: int) -> int:
Java
public int countTriples(int n)