ForHosting KIT · Math

Sum of first n squares

Add the first n squares in one step with n(n + 1)(2n + 1)/6: 1² + 2² + 3² + 4² + 5² is 55, and every n from 0 to ten million gives an exact whole number. Free in your browser.

  • Free
  • Runs in your browser
  • No sign-up
  • Your data stays on your device
Use it from WebAPIEmailTelegramApp soon
If you enter
Non-negative integer n
5
You get
Value
55
Sum
55

The sum of the first n squares is one of the classic closed forms every algebra and discrete-mathematics student meets early: one squared plus two squared plus three squared and so on up to n squared equals n times n plus one times two-n plus one, all divided by six. That identity turns a loop of multiplications into a single cubic polynomial that is always exact for integer n. This calculator evaluates the formula with deterministic integer arithmetic for any accepted non-negative index, rejects negative n with a clear validation error, and returns both the numeric sum and the formula string so homework checkers, scripts, and lesson pages can trust a single contract.

The formula for the sum of the first n squares

By definition the sum of the first n squares is S(n) equals one squared plus two squared plus three squared continuing through n squared. Expanding that sum with the standard finite-series identity produces the compact closed form S(n) equals n times the quantity n plus one times the quantity two n plus one, then divided by six.

Substituting small non-negative integers recovers the familiar table: S(0) is zero for the empty sum, S(1) is one, S(2) is five, S(3) is fourteen, S(4) is thirty, S(5) is fifty-five, S(6) is ninety-one, S(7) is one hundred forty, S(8) is two hundred four, S(9) is two hundred eighty-five, and S(10) is three hundred eighty-five.

Division by six is always exact for integer n because among n, n plus one, and two n plus one there is always a factor of two and a factor of three, so the product is divisible by six with no remainder. Algebra courses use this identity when they introduce induction proofs, discrete-mathematics courses use it for figurate numbers and counting arguments, and programming contests use it whenever a solution needs a constant-time sum of squares instead of an O(n) loop.

Understanding that the result is a cubic polynomial in n, not a recurrence you must iterate from one, is the first step to using the formula confidently in code, on paper, and in teaching materials that explain why the polynomial works.

Practical use, limits, pricing, and delivery

Use the browser tool when you want an immediate sum of squares while studying series formulas, checking a contest answer, or explaining the n(n+1)(2n+1)/6 identity without installing a computer-algebra system. Accepted indexes run from zero through ten million so empty-sum edge cases, large teaching demos, and Project-Euler-scale tables remain feasible without unbounded memory growth. Results are fully deterministic: the same n always yields the same sum and the same value echo.

There is no language model, no external arithmetic service, and no randomness, so the capability is safe to pin in continuous-integration checks and reproducible lesson materials.

Homework and contest verification

Students and graders confirm 1²+2²+…+n² for a given n without a local computer-algebra system or hand-written loops.

Algebra and series lesson demos

Lesson pages that introduce the sum-of-squares formula can show n(n+1)(2n+1)/6 beside worked values for small n.

Scripts and CI oracles

Automated checkers and interview sandboxes call a deterministic closed form instead of reimplementing the sum of squares in every language.

What is the formula for 1² + 2² + … + n²?

The standard closed form is n(n+1)(2n+1)/6. This calculator evaluates that polynomial with exact integer arithmetic.

What happens if n is negative?

Negative n is not allowed; only non-negative integers are accepted and validation failures are not charged.

Is the sum of first n squares calculator free?

The browser tool runs free on your device.

Why use the closed form instead of looping over squares?

The polynomial is exact for integer n and runs in constant time after validation. This calculator evaluates it with BigInt so large indexes stay precise without summing millions of terms.

Why does the division by six always come out exact?

Among n, n + 1 and 2n + 1 there is always a multiple of 2 and always a multiple of 3, so their product divides by 6 with nothing left over. The result is a whole number for every whole n.

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.

POSThttps://api.kit.forhosting.com/math/sum-of-squares

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.

curl -X POST https://api.kit.forhosting.com/math/sum-of-squares \
  -H "Authorization: Bearer $KIT_KEY" \
  -H "Content-Type: application/json" \
  -d '{"n":5}'
{
  "n": 5
}
{
  "task_id": "tsk_a1b2c3d4e5f6a1b2c3d4e5f6",
  "type": "math.sum_of_squares",
  "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.

Per request$0.002

Published price — no tokens, no invented credits. A failed task is never charged.

max_n10000000
HTTPCodeMeaning
401unauthorizedMissing or invalid API key.
402insufficient_balanceYour balance doesn't cover the task price.
404unknown_typeThat task type doesn't exist.
429rate_limitedToo many requests. Use the webhook instead of polling.

How this calculator validates n and evaluates the closed form

The engine checks that n is present as a finite integer, is not negative, and stays within the published upper bound of ten million, then returns sum equal to n times n-plus-one times two-n-plus-one over six, together with the formula string for documentation and a value field that echoes the same result so callers who prefer a generic name see both labels. There is no loop over squares on the hot path: the closed form is constant time after validation. Send a JSON body with the required integer field n. Negative n fails immediately with invalid_input and is not charged; the same error path covers missing n, non-integers, non-finite numbers, and values above the published maximum. Arithmetic uses BigInt so intermediate products never lose low-order bits before the exact division by six, and when the final result fits inside the safe JSON integer range it is returned as a number rather than a decimal string.

Practical use, limits, pricing, and delivery

Each successful request is one billable item at $0.002; failed validation does not consume balance. Wire the endpoint into algebra demos, coding-interview sandboxes, discrete-math tutorials, or integer preprocessing steps where you already prefer exact closed-form arithmetic over approximate heuristics or opaque black-box calculators that cannot show the formula or the definition as one squared plus two squared up through n squared.

How do I compute the sum of the first n squares with this API?

Send a JSON body with non-negative integer field n to math.sum_of_squares. The response returns sum = n*(n+1)*(2*n+1)/6, the formula string, and value (same as sum).

Read the full KIT documentation →