Revenge of Pythagoras

Time limit1sMemory limit128 MB

Summary
Given a natural number A, count the values B > A such that A, B and hypotenuse C form a right triangle with natural C.
Level

Medium7 of 10

Topics
Number theory, Math, Brute force, Implementation
Solved
No attempts yet

Problem

The Pythagorean theorem describes the relationship among the three sides of a right triangle. If the hypotenuse has length CC and the other two sides have lengths AA and BB, then

A2+B2=C2A^2 + B^2 = C^2

Among right triangles whose three side lengths are all natural numbers, the best known one is the triangle with sides 33, 44, and 55. For example, when A=12A = 12 there are exactly two such right triangles.

Given a natural number AA, how many natural numbers BB with B>AB > A are there such that the hypotenuse length CC is also a natural number?

Input

The input consists of several test cases. Each test case is given on a single line as one natural number AA (2≤A<2202 \le A < 2^{20}). The last line of the input contains a single 00, which is not processed.

Output

For each test case, print on its own line the number of natural numbers BB (with B>AB > A) that form a right triangle whose hypotenuse length is a natural number.

Examples1

  1. Example 1

    Input
    3
    12
    2
    1048574
    1048575
    0
    
    Expected output
    1
    2
    0
    1
    175