Sum of three squares representation calculator
This sum of three squares representation calculator takes a non-negative integer and finds explicit integers a, b, and c such that n = a² + b² + c².
Run — free
It also applies Legendre's three-square theorem before searching, so an integer of the forbidden form 4^a(8b+7) produces a clear error instead of a fruitless calculation. The result includes the three terms, a readable equation, and a verification flag, making it useful for study, software tests, and exact number-theory workflows.
Turn an existence theorem into an explicit representation
Legendre's three-square theorem gives a complete test for whether a non-negative integer can be written as the sum of three integer squares. Knowing that a representation exists, however, is often only the beginning. Exercises, demonstrations, test fixtures, and computational investigations usually need the actual values. Enter n and the calculator returns a deterministic triple in nondecreasing search order, together with an equation you can inspect directly. Zero is accepted, and zero terms are allowed because a representation may naturally use fewer than three nonzero squares. For example, a perfect square can be returned with two zero terms. The returned squares array contains the bases rather than their squared values, so an array such as [1, 2, 3] means 1² + 2² + 3². The accompanying equation removes ambiguity, while the verification field confirms that the integer arithmetic was checked before the response was produced. Repeated calls with the same input return the same representation, which makes the endpoint suitable for reproducible documentation and automated tests.
Understand the forbidden 4^a(8b+7) form
The only non-negative integers that cannot be expressed as three squares are those that can be written as 4^a(8b+7), where a and b are non-negative integers. The calculator checks this condition by repeatedly removing factors of four and then examining the remainder modulo eight. If that reduced value is congruent to seven, no representation exists, and the request returns an invalid-input error that names the forbidden form. This is a mathematical impossibility result, not a search timeout or an inconclusive answer. As quick examples, 7 is forbidden directly, 28 is forbidden because it is 4 × 7, and 112 remains forbidden after removing two factors of four. In contrast, a number that merely contains a factor of four is not automatically excluded; its reduced part must also be seven modulo eight. Performing the theorem check first gives callers a precise distinction between malformed input, unsupported size, and a valid integer that provably has no solution. It also prevents needless iteration over candidates when mathematics already settles the question.
Use deterministic output safely in programs
Send n as a JSON integer or as a plain decimal integer string. Strings are useful when a form control naturally supplies text, but signs, whitespace, decimal points, separators, and scientific notation are rejected so the meaning stays exact. Inputs are bounded at the published maximum to keep browser and API execution predictable. For admissible values, the algorithm scans the first square in ascending order and solves the remaining two-square problem with opposing integer pointers. It uses no network access, random choices, clocks, or mutable shared state. That makes both the successful triple and every validation decision stable across repeated runs. In an application, read the three entries from squares and independently compute a² + b² + c² if you want a local assertion; the equation is intended for display. Treat a forbidden-form response as a domain result communicated through the standard invalid-input error, rather than retrying it. The browser version runs the same pure solver as the API. Interactive use is free on this page, while an API request costs $0.002, which is convenient when generating examples or checking many individually selected values from a larger workflow.
What you can do with it
Complete a number theory exercise
Find concrete square terms after using Legendre's theorem to establish that a representation exists.
Generate deterministic test fixtures
Create reproducible triples and readable equations for software that validates sums of squares.
Classify impossible inputs
Identify numbers in the forbidden 4^a(8b+7) family with an explicit mathematical error.
FAQ
What does the capability return?
It returns n, an array containing the three square bases, a readable equation, and a verification flag.
Can every non-negative integer be represented?
No. Exactly the integers of the form 4^a(8b+7) cannot be written as a sum of three squares.
Are zero terms allowed?
Yes. The theorem concerns three integer squares, and one or more of those integers may be zero.
Will repeated requests return the same triple?
Yes. The search order is deterministic and uses no randomness, network data, or current time.
How much does an API request cost?
Each API request costs $0.002. The calculator can also run free in your browser on this page.
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-three-squares-rep \
-H "Authorization: Bearer $KIT_KEY" \
-H "Content-Type: application/json" \
-d '{"n":310}'const res = await fetch("https://api.kit.forhosting.com/numth/sum-three-squares-rep", {
method: "POST",
headers: {
"Authorization": `Bearer ${process.env.KIT_KEY}`,
"Content-Type": "application/json"
},
body: JSON.stringify({
"n": 310
})
});
const { task_id } = await res.json();import os, requests
res = requests.post(
"https://api.kit.forhosting.com/numth/sum-three-squares-rep",
headers={"Authorization": f"Bearer {os.environ['KIT_KEY']}"},
json={
"n": 310
},
)
task_id = res.json()["task_id"]<?php
$res = file_get_contents("https://api.kit.forhosting.com/numth/sum-three-squares-rep", false, stream_context_create([
"http" => [
"method" => "POST",
"header" => "Authorization: Bearer " . getenv("KIT_KEY") . "\r\nContent-Type: application/json",
"content" => '{"n":310}',
],
]));
$task = json_decode($res, true);body := bytes.NewBufferString(`{"n":310}`)
req, _ := http.NewRequest("POST", "https://api.kit.forhosting.com/numth/sum-three-squares-rep", 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": 310
}Example response
{
"task_id": "tsk_a1b2c3d4e5f6a1b2c3d4e5f6",
"type": "numth.sum_three_squares_rep",
"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 | 10000000 |
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. |