Sum of Two Squares Count
The sum of two squares count returns r₂(n), the exact number of ordered integer pairs (x, y) satisfying x² + y² = n.
Run — free
Order and signs matter, so (1, 2), (2, 1), (-1, 2), and their other sign variants are separate representations. Enter any non-negative integer within the published limit to receive a deterministic count derived from its prime factorization, without enumerating every possible coordinate pair. The result is suitable for direct use in calculations and automated checks.
What the ordered representation count means
This calculator answers a precise number-theory question: how many pairs of integers (x, y) satisfy x² + y² = n? The word ordered is important. If x and y are different, swapping them produces another representation. Signs also count independently whenever a coordinate is nonzero. For example, a geometric point in one quadrant may correspond to several signed points around a circle, and the calculator includes all of them. Axis points are included as well, so a perfect square receives representations such as (a, 0), (-a, 0), (0, a), and (0, -a). The special value n = 0 has exactly one representation, (0, 0). The returned field r_two follows the standard notation r₂(n). It is a count, not a list of pairs, which makes the result useful even when n has many representations. Inputs must be non-negative integers. Decimal values, unsafe integers, negative numbers, and values above the declared bound are rejected instead of being rounded or interpreted silently. This keeps the mathematical statement and the API result unambiguous.
How r₂(n) is calculated
The algorithm factors n and applies the classical sum-of-two-squares counting theorem. For positive n, r₂(n) equals four times the difference between the number of divisors congruent to 1 modulo 4 and the number congruent to 3 modulo 4. An equivalent factorized form is faster to evaluate: if any prime congruent to 3 modulo 4 occurs to an odd exponent, the count is zero. Otherwise, multiply one plus the exponent for every prime factor congruent to 1 modulo 4, then multiply the product by four. Powers of 2 do not change that product. Consider n = 65 = 5 × 13. Both primes are 1 modulo 4 and each has exponent one, so r₂(65) = 4 × 2 × 2 = 16. The implementation performs deterministic integer division and never searches a coordinate grid, calls a remote service, or uses probabilistic primality tests. Its declared maximum bounds the trial-factorization loop. Because every accepted value is a safe JavaScript integer and intermediate count values remain exact within this input range, the returned integer is stable across the API and browser execution paths.
Using the result correctly
Use r_two when you need the full signed, ordered count used in standard arithmetic formulas, lattice-point problems, and coefficient calculations for theta series. Do not treat it as the number of essentially different decompositions into two non-negative squares. If you only care about combinations with 0 ≤ x ≤ y, you must account for symmetry separately: a pair with two distinct nonzero coordinates contributes eight ordered signed representations, a pair on an axis contributes four, and a pair with equal nonzero coordinates contributes four. Those orbit sizes explain why positive r₂(n) values are normally multiples of four. The zero case is the exception because only the origin qualifies. The capability reports a zero cleanly when no representation exists, which is distinct from an input error. For automated work, send the integer in the n field and read the r_two field from the response. Each API request costs $0.002; the browser version uses the same pure calculation. For reproducible datasets, retain n beside r_two so the meaning of each count remains explicit and future checks can rerun the exact same input.
What you can do with it
Check a number-theory exercise
Verify the ordered signed representation count obtained from a prime factorization or divisor argument.
Count lattice points on a circle
Find how many integer-coordinate points lie on the circle x² + y² = n without enumerating a square grid.
Generate arithmetic sequence data
Compute exact r₂(n) values for selected integers used in experiments, tests, or theta-series coefficients.
FAQ
Are (x, y) and (y, x) counted separately?
Yes. The result counts ordered pairs, so swapped coordinates are distinct unless x equals y.
Do negative coordinates count?
Yes. Every signed integer pair satisfying the equation is included.
What is r₂(0)?
It is 1 because (0, 0) is the only ordered integer pair whose squares sum to zero.
Why can the result be zero?
A positive integer has no two-square representation when some prime congruent to 3 modulo 4 has an odd exponent in its factorization.
Does the calculator return the actual pairs?
No. It returns only the exact count r_two, using factorization rather than enumerating coordinates.
What does an API request cost?
Each request costs $0.002. The calculation is also available in the browser.
For developers — API access
Everything on this page is available programmatically. This section is for teams who want to wire it into their own systems; everyone else can just use the tool above.
API endpoint
Prefer to automate it? One authenticated POST creates the task; the result comes back by webhook or a signed link. The same capability also runs here on the web, by email and from Telegram — and soon from our app too.
Call it from your stack
curl -X POST https://api.kit.forhosting.com/numth/sum-two-squares-count \
-H "Authorization: Bearer $KIT_KEY" \
-H "Content-Type: application/json" \
-d '{"n":65}'const res = await fetch("https://api.kit.forhosting.com/numth/sum-two-squares-count", {
method: "POST",
headers: {
"Authorization": `Bearer ${process.env.KIT_KEY}`,
"Content-Type": "application/json"
},
body: JSON.stringify({
"n": 65
})
});
const { task_id } = await res.json();import os, requests
res = requests.post(
"https://api.kit.forhosting.com/numth/sum-two-squares-count",
headers={"Authorization": f"Bearer {os.environ['KIT_KEY']}"},
json={
"n": 65
},
)
task_id = res.json()["task_id"]<?php
$res = file_get_contents("https://api.kit.forhosting.com/numth/sum-two-squares-count", false, stream_context_create([
"http" => [
"method" => "POST",
"header" => "Authorization: Bearer " . getenv("KIT_KEY") . "\r\nContent-Type: application/json",
"content" => '{"n":65}',
],
]));
$task = json_decode($res, true);body := bytes.NewBufferString(`{"n":65}`)
req, _ := http.NewRequest("POST", "https://api.kit.forhosting.com/numth/sum-two-squares-count", body)
req.Header.Set("Authorization", "Bearer "+os.Getenv("KIT_KEY"))
req.Header.Set("Content-Type", "application/json")
res, _ := http.DefaultClient.Do(req)Example request
{
"n": 65
}Example response
{
"task_id": "tsk_a1b2c3d4e5f6a1b2c3d4e5f6",
"type": "numth.sum_two_squares_count",
"status": "queued",
"_links": {
"result": "/tasks/tsk_…/result"
}
}The API is asynchronous: the call returns a task_id immediately and the result arrives by webhook. Polling is capped at 1 req/s per task.
Pricing
Published price — no tokens, no invented credits. A failed task is never charged.
Limits
max_n | 1000000000000 |
Errors
| HTTP | Code | Meaning |
|---|---|---|
401 | unauthorized | Missing or invalid API key. |
402 | insufficient_balance | Your balance doesn't cover the task price. |
404 | unknown_type | That task type doesn't exist. |
429 | rate_limited | Too many requests. Use the webhook instead of polling. |