Problem 545
Pythagorean Triplets Count
Medium · 20 pts · Math, Number Theory
Problem
Count the triples of integers (a, b, c) with 1 <= a < b < c <= N and a² + b² = c². Checking every pair (a, b) is too slow for N = 100000. Euclid's formula produces every primitive triple exactly once as (m² - n², 2mn, m² + n²), where m > n >= 1, gcd(m, n) = 1 and m - n is odd. Every other triple is a multiple k of a primitive one, so a primitive triple with hypotenuse c contributes floor(N / c) triples.
Input
A single integer N.
Output
Print the number of Pythagorean triples with all sides at most N.
Constraints
1 <= N <= 100000
Example input
13
Example output
3
Explanation
For N = 13 the triples are (3, 4, 5), (6, 8, 10) and (5, 12, 13). The next one, (9, 12, 15), has a side larger than 13, so the answer is 3.