Waring problem representation
With squares, 30 becomes 25 + 4 + 1, that is 5² + 2² + 1², by always taking the largest square that still fits. Choose a number and a power of two or more to see each term and a sum that matches. Free in your browser.
- Free
- Runs in your browser
- No sign-up
- Your data stays on your device
Run — free
Worked example
- N
- 30
- K
- 2
- Bases
- 5, 2, 1
- Term count
- 3
- Sum
- 30
- Representation
- 5^2 + 2^2 + 1^2
Waring's classical question asks how every natural number can be written as a sum of k-th powers of non-negative integers. This capability implements the standard greedy construction: given a non-negative integer n and an exponent k of at least two, it repeatedly subtracts the largest perfect k-th power that does not exceed the remainder until nothing is left. You receive the ordered list of bases, each term's value, a display string such as five squared plus two squared plus one squared, and a sum that equals n for every successful call. Limits keep n and k inside safe integer arithmetic for edge workers without multiprecision libraries.
What greedy Waring-style representation means
A k-th power is an integer of the form m raised to the power k, where m is a non-negative integer and k is a fixed exponent. Waring's problem in number theory studies the least number g(k) such that every natural number is a sum of at most g(k) such powers; famous theorems give g(2) equals four (Lagrange's four-square theorem) and explicit bounds for higher k.
Instead it applies the transparent greedy heuristic used in textbooks and programming contests: while the remainder r is positive, choose the largest m with m to the k at most r, record that base, and replace r with r minus m to the k. Because one to the k always equals one for k greater than or equal to one, every non-negative integer has a finite greedy expansion and the loop always terminates with sum equal to n.
The greedy expansion is not always minimal—twenty-three as cubes is a classic illustration where greed uses two cubes of two and seven ones, while other identities use fewer cubes—but it is unique as a procedure, fully deterministic, and easy to audit term by term. Zero is represented as an empty sum with the display string zero. Perfect powers collapse to a single term: one hundred with k equals two yields only ten squared.
What you enter and what you get back
A short formula field restates the greedy rule so UI copy can sit next to the numbers without inventing prose. Integer k-th roots are computed by a verified binary search over integer multiplies so floating Math.pow is only a seed, never the sole authority. A hard cap of ten thousand terms protects the response size if extreme remainders ever stacked ones; with the published n limit that guard is a safety belt rather than a common path.
Example: n equals thirty and k equals two returns bases five, two, one and the string five squared plus two squared plus one squared.
What you can do with it
Classroom Waring demos
Show students how greed builds a sum of squares or cubes for any n, with each base and value listed for board checks.
Contest and LMS oracles
Mark programming tasks that accept any valid k-th power expansion by comparing against this deterministic greedy reference.
FAQ
How does the Waring problem representation calculator work?
Send non-negative integer n and exponent k >= 2.
Is the greedy expansion always the shortest?
No. Greed is unique as a procedure and always correct as a sum, but shorter expansions may exist (for example some cubes).
How is 30 written as a sum of squares?
The largest square not above 30 is 25, leaving 5. Then 4 fits, leaving 1, and finally 1. So 30 = 5² + 2² + 1², three terms that add back to 30.
What happens when the number is already a perfect power?
It comes back as a single term. With squares, 100 is simply 10², because the largest square that fits is the whole number and nothing is left over.
What is the result for zero?
Zero is shown as an empty sum, written simply as zero. No power needs to be taken away, so the list of terms is empty and the total still matches.
Related tools
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/waring-representation \
-H "Authorization: Bearer $KIT_KEY" \
-H "Content-Type: application/json" \
-d '{"n":30,"k":2}'const res = await fetch("https://api.kit.forhosting.com/numth/waring-representation", {
method: "POST",
headers: {
"Authorization": `Bearer ${process.env.KIT_KEY}`,
"Content-Type": "application/json"
},
body: JSON.stringify({
"n": 30,
"k": 2
})
});
const { task_id } = await res.json();import os, requests
res = requests.post(
"https://api.kit.forhosting.com/numth/waring-representation",
headers={"Authorization": f"Bearer {os.environ['KIT_KEY']}"},
json={
"n": 30,
"k": 2
},
)
task_id = res.json()["task_id"]<?php
$res = file_get_contents("https://api.kit.forhosting.com/numth/waring-representation", false, stream_context_create([
"http" => [
"method" => "POST",
"header" => "Authorization: Bearer " . getenv("KIT_KEY") . "\r\nContent-Type: application/json",
"content" => '{"n":30,"k":2}',
],
]));
$task = json_decode($res, true);body := bytes.NewBufferString(`{"n":30,"k":2}`)
req, _ := http.NewRequest("POST", "https://api.kit.forhosting.com/numth/waring-representation", 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": 30,
"k": 2
}Example response
{
"task_id": "tsk_a1b2c3d4e5f6a1b2c3d4e5f6",
"type": "numth.waring_representation",
"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 | 1000000000 |
max_k | 40 |
max_terms | 10000 |
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. |
Technical notes
Powers below two are rejected with invalid_input rather than silently treating first powers as a trivial sum of ones.
What greedy Waring-style representation means
This endpoint does not search for a shortest representation or prove g(k).
Input contract, response fields, and validation
POST a JSON body with two required integers: n, the non-negative target in zero through one billion, and k, the exponent with minimum two and maximum forty. Aliases number for n and power for k are accepted for ergonomic clients. Missing fields, non-integers, fractions, negative n, k below two, or values above the published ceilings fail with invalid_input naming the problem; those failures are not billed. On success the payload echoes n and k, lists bases in the order greed produced them (non-increasing), expands terms as objects with base, power, and value, reports term_count and sum (always equal to n), and builds a human-readable representation string joining each base caret power with plus signs.
Where a greedy sum-of-powers API earns its keep
Number-theory courses and olympiad prep need a live oracle that turns an abstract Waring discussion into concrete bases students can check by hand. Keep n and k inside the documented limits, pin representation and bases in tests, and you get reproducible Waring-style expansions for homework, LMS plugins, and edge workers without standing up symbolic math infrastructure of your own. Coding platforms use the same greedy loop as a reference solution for problems that ask for any valid expansion rather than a minimal one, and embedding a tiny parse API avoids shipping multiprecision or CAS stacks into graders. Research notebooks comparing greedy length against known g(k) bounds can batch many (n, k) pairs through the API and log term_count without reimplementing root-finding carefully for every language.
Integration use cases
Minimal-length research baselines
Batch-log greedy term_count against known g(k) bounds when exploring how far greed sits from optimal Waring length.
Technical questions
Why is k required to be at least two?
Waring-style k-th power questions start at squares (k=2). First powers would only split n into ones; k below two is rejected with invalid_input and is not billed.
What does the response contain?
Echoed n and k, bases in greedy order, terms with base/power/value, term_count, sum equal to n, a representation string, and a short formula label.
What does the Waring representation tool cost?
The browser tool is free. API requests use prepaid KIT balance at $0.002 per successful call; invalid input is not billed.