Square pyramidal number
Get the n-th square pyramidal number, the count of balls stacked in a square-based pyramid: for n of 5 it is 55, the sum of the first five squares. Exact whole-number results for any n from 1 to ten million. Free in your browser.
- Free
- Runs in your browser
- No sign-up
- Your data stays on your device
Run — free
Worked example
- N
- 5
- Value
- 55
- Sum of squares
- 55
The square pyramidal numbers count cannonballs stacked in a square pyramid, or equivalently the sum of the first n positive squares. The closed formula is exact and elementary: multiply n by n plus one by two-n plus one, then divide by six. That identity produces the familiar sequence 1, 5, 14, 30, 55, 91, 140 for n starting at one. This calculator evaluates the n-th term for any accepted positive index using integer arithmetic, never a floating-point guess, so results stay bit-exact for large n. Values of n below one are rejected with a clear validation error rather than returning zero or a silent empty sum.
What square pyramidal numbers are and how P(n) is defined
A square pyramidal number is the number of unit balls you need to build a pyramid whose base is an n-by-n square and whose successive layers are (n−1)-by-(n−1) squares, down to a single ball at the apex. Algebraically that stack is exactly the sum of the first n positive squares: one squared plus two squared plus three squared and so on up to n squared. Expanding the sum with the classical formula for the sum of squares yields the compact polynomial P(n) = n(n + 1)(2n + 1) / 6.
Substituting small positive integers recovers the standard table: P(1) = 1, P(2) = 5, P(3) = 14, P(4) = 30, P(5) = 55, P(6) = 91, P(7) = 140, P(8) = 204, P(9) = 285, P(10) = 385. Division by six is always exact for integer n because among n, n + 1, and 2n + 1 there is always a factor of two and a factor of three.
Contests, discrete-mathematics courses, and recreational number-theory problems use these values constantly: cannonball problems, figurate-number tables, and identities that relate pyramidal numbers to binomial coefficients all depend on evaluating this closed form rather than looping over squares by hand. Understanding that the sequence is a cubic polynomial, not a recurrence you must iterate from one, is the first step to using it confidently in code, on paper, and in teaching materials.
What you can do with it
Homework and contest verification
Students and graders confirm the n-th square pyramidal number or the sum of the first n squares without a local computer-algebra system.
Figurate-number tables and demos
Lesson pages and interactive demos that introduce cannonball pyramids can show P(n)=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 formula in every language.
FAQ
Is the square pyramidal number the same as the sum of the first n squares?
Yes. By definition P(n) equals 1² + 2² + … + n², and the closed form n(n+1)(2n+1)/6 is the standard evaluation of that sum.
What happens if n is below 1?
Only positive integers n ≥ 1 are accepted; zero and negative n are rejected and not charged.
Is the square pyramidal number 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.
How many cannonballs are in a square pyramid with 5 layers?
There are 55. A pyramid with a 5-by-5 base has one ball on top and, below it, layers of 2-by-2, 3-by-3, 4-by-4 and 5-by-5, which add up to 55.
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/math/pyramidal-number \
-H "Authorization: Bearer $KIT_KEY" \
-H "Content-Type: application/json" \
-d '{"n":5}'const res = await fetch("https://api.kit.forhosting.com/math/pyramidal-number", {
method: "POST",
headers: {
"Authorization": `Bearer ${process.env.KIT_KEY}`,
"Content-Type": "application/json"
},
body: JSON.stringify({
"n": 5
})
});
const { task_id } = await res.json();import os, requests
res = requests.post(
"https://api.kit.forhosting.com/math/pyramidal-number",
headers={"Authorization": f"Bearer {os.environ['KIT_KEY']}"},
json={
"n": 5
},
)
task_id = res.json()["task_id"]<?php
$res = file_get_contents("https://api.kit.forhosting.com/math/pyramidal-number", false, stream_context_create([
"http" => [
"method" => "POST",
"header" => "Authorization: Bearer " . getenv("KIT_KEY") . "\r\nContent-Type: application/json",
"content" => '{"n":5}',
],
]));
$task = json_decode($res, true);body := bytes.NewBufferString(`{"n":5}`)
req, _ := http.NewRequest("POST", "https://api.kit.forhosting.com/math/pyramidal-number", 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": 5
}Example response
{
"task_id": "tsk_a1b2c3d4e5f6a1b2c3d4e5f6",
"type": "math.pyramidal_number",
"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. |
Technical notes
How this calculator computes P(n) and validates input
There is no loop over squares on the hot path: the closed form is O(1) after validation. Send a JSON body with the required integer field n. The engine validates that n is present as a finite integer, at least one, and within the published upper bound of ten million, then returns value equal to n times n-plus-one times two-n-plus-one over six, together with the formula string for documentation and a sum_of_squares field that echoes the same result so callers who think in terms of the definition rather than the figurate name see both labels. Values of n below one fail immediately with invalid_input and are 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
Use the browser tool when you want an immediate P(n) while studying figurate numbers, checking a contest answer, or explaining the sum-of-squares formula without installing a computer-algebra system. Accepted indexes run from one through ten million so large teaching demos and Project-Euler-scale tables remain feasible without unbounded memory growth. 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. Each successful request is one billable item at $0.002; failed validation does not consume balance. Results are fully deterministic: the same n always yields the same value and the same sum_of_squares echo. Wire the endpoint into number-theory demos, coding-interview sandboxes, discrete-math tutorials, or integer preprocessing steps where you already prefer exact figurate arithmetic over approximate heuristics or opaque black-box calculators that cannot show the formula or the definition as the sum of the first n squares.
Technical questions
How do I compute the n-th square pyramidal number with this API?
Send a JSON body with positive integer field n to math.pyramidal_number. The response returns value = n*(n+1)*(2*n+1)/6, the formula string, and sum_of_squares (same as value).