What is a square pyramidal number?
A square pyramidal number counts the total number of balls (or unit spheres or cubes) you get when you stack them into a pyramid with a square base. The top layer is a single ball, the next layer is a 2x2 = 4 square, the third is 3x3 = 9, and the bottom layer of n layers is n x n balls. Adding every layer gives the square pyramidal number P(n). The sequence begins 0, 1, 5, 14, 30, 55, 91, 140, ... and is catalogued as OEIS A000330.
How to use this calculator
Enter the number of stacked layers \(n\) (a non-negative whole number) and the calculator returns \(P(n)\), the total number of balls in the complete stack. Use it for cannonball stacking puzzles, orange or fruit displays, classroom number-theory exercises, or any time you need the sum of the first \(n\) perfect squares quickly.
The formula explained
By definition \(P(n)\) is the sum of the first \(n\) square numbers: \(P(n) = 1^2 + 2^2 + \cdots + n^2\). This sum has a neat closed form:
$$P(n) = \frac{n(n + 1)(2n + 1)}{6}$$The product \(n(n + 1)(2n + 1)\) is always divisible by 6, so for any whole number \(n\) the result is an exact integer. \(P(0) = 0\) represents an empty stack, and negative layer counts are not physically meaningful (they are treated as 0 here).
Worked example
Suppose you stack 4 layers. By summing squares: \(1 + 4 + 9 + 16 = 30\). Using the closed form: $$\frac{4 \times 5 \times 9}{6} = \frac{180}{6} = 30 \text{ balls}.$$ A larger check: for \(n = 10\), the result is $$\frac{10 \times 11 \times 21}{6} = \frac{2310}{6} = 385 \text{ balls}.$$
FAQ
What happens at \(n = 0\)? \(P(0) = 0\), an empty pyramid with no balls.
Can \(n\) be a fraction? The closed-form expression still evaluates, but a square pyramidal number is only physically meaningful for whole, non-negative layer counts.
How fast does it grow? For large \(n\) the value grows roughly like \(n^3\) divided by 3, so very tall stacks contain enormous numbers of balls.