Lagrange's Four-Square Theorem
Time limit2sMemory limit128 MB
For each input number, count the unordered ways to express it as a sum of one to four positive perfect squares.
- Level
Medium4 of 10
- Topics
- Brute force, Math, Combinatorics
- Solved
- No attempts yet
Problem
Every positive integer can be written as a sum of at most four squares. This fact is known as Lagrange's four-square theorem, proved by Joseph-Louis Lagrange in 1770.
We do not need to prove the theorem or discover a new one. Instead, given , we want to count the number of ways to write as a sum of at most four positive squares. Two representations that differ only in the order of the squares are considered the same, so and count as one.
For example, when there are three such representations: , , and .
Input
The input contains at most lines. Each line holds a single positive integer smaller than . The last line contains a single , which is not part of the data.
Output
For each input value , output the number of ways to write as a sum of at most four positive squares (order does not matter), one per line.