A truncated square-pyramid of height \$h\$ has \$h\$ square layers where each layer has a side \$1\$ greater than the one above it, apart from the top layer which is a square of blocks with a given side length.
Here is a truncated square-pyramid of height \$7\$ and top side-length \$4\$ viewed from the side:
side length blocks
████ 4 16
▐████▌ 5 25
██████ 6 36
▐██████▌ 7 49
████████ 8 64
▐████████▌ 9 81
██████████ 10 100
Total = 371
It requires \$\sum_{i=4}^{10}i^2=371\$ blocks to construct.
A truncated square-pyramid garden of size \$N\$ consists of truncated, square-pyramids of heights \$1\$ to \$N\$ where the \$n^{th}\$ tallest pyramid has a top side-length of \$x\$ where \$x\$ is the remainder after dividing \$N\$ by \$n\$ unless there is no remainder in which case \$x=n\$.
Here is a side-on view of a pyramid garden with \$N=6\$ pyramids, arranged from tallest to shortest:
N=6 ▐▌
██ ██
▐██▌ ▐██▌ ▐██▌
████ ████ ████ ██
▐████▌ ▐████▌ ▐████▌ ▐██▌ ▐▌
██████ ██████ ██████ ████ ██ ██████
height 6 5 4 3 2 1
n 1 2 3 4 5 6
remainder of N/n 0 0 0 2 1 0
top side-length 1 2 3 2 1 6
This garden takes \$337\$ blocks to construct.
Task
Given a positive integer, \$N\$, calculate the number of blocks required to build a truncated square-pyramid garden of size \$N\$.
You are not required to handle size zero (just in case that causes an edge case for anyone).
Since this is a sequence, you may instead opt to output the block counts for all gardens up to size \$N\$ (with or without a leading zero, for size zero), or generate the block counts indefinitely without input (again a leading zero is acceptable). It's not currently in the OEIS, and neither is its two-dimensional version.
This is code-golf, so the shortest code in each language wins.
Test cases
The first \$100\$ terms of the sequence are (starting with \$N=1\$):
1, 9, 28, 80, 144, 337, 455, 920, 1251, 1941, 2581, 4268, 4494, 7065, 9049, 11440, 13299, 19005, 20655, 28544, 31140, 37673, 45305, 59360, 59126, 73289, 86256, 101124, 109647, 136805, 138364, 170780, 184520, 211485, 241157, 275528, 272869, 326729, 368320, 414692, 424823, 499261, 510708, 596140, 636361, 680537, 753508, 867036, 857345, 966889, 1027920, 1130172, 1197747, 1358369, 1393684, 1528840, 1571095, 1712605, 1860668, 2083248, 2023267, 2261821, 2445122, 2584136, 2685714, 2910217, 2980225, 3298056, 3459910, 3719313, 3824917, 4206640, 4128739, 4534965, 4846194, 5081240, 5308615, 5695545, 5827090, 6349936, 6395099, 6753185, 7173903, 7783720, 7688846, 8192521, 8679955, 9202980, 9429730, 10177969, 10090513, 10725680, 11134432, 11766133, 12407705, 13134004, 13024244, 13979357, 14523352, 15111244
Note: there is a surprisingly terse Jelly solution to find for this one, happy hunting!
Here's a hint that might help get you thinking about a different approach:
In Jelly lists are 1-indexed and indexing is modular, so finding the top side-length could be calculated by using \$N\$ to index into a list of
[1,2,...,n]
.
2022-03-15: If you just want to see this solution I have now posted it below.