# Square pyramidal numbers

A000330 - OEIS

Your task is simple, generate a sequence that, given index i, the value on that position is the sum of squares from 0 upto i where i >= 0.

# Example:

Input: 0
Output: 0           (0^2)

Input: 4
Output: 30          (0^2 + 1^2 + 2^2 + 3^2 + 4^2)

Input: 5
Output: 55          (0^2 + 1^2 + 2^2 + 3^2 + 4^2 + 5^2)


# Specification:

• You may take no input and output the sequence indefinitely;
• You may take input N and output the Nth element of the sequence;
• You may take input N and output the first N elements of the sequence.
• Fun observation from OEIS: This sequence contains exactly two perfect squares: f(1) == 1 * 1 (1), and f(24) == 70 * 70 (4900). Oct 17, 2017 at 16:31
• May we begin the sequence at f(1) = 1? Oct 18, 2017 at 6:10
• @Emigna sorry but no, you need to start from f(0) = 0. i've pointed out that to the few answers that failed that requirement Oct 18, 2017 at 9:44
• The f(0) = 0 requirement ruined a few of my solutions :( Oct 19, 2017 at 5:19

# Gaia, 3 bytes

s¦Σ


Try it online!

{print$1^3/3+$1^2/2+$1/6}  # dc, 15 12 bytes d1+dd+1-**6/  This is simply the factorised Faulhaber polynomial: n(n+1)(2n+1)/6, except that the (2n+1) term is calculated as 2(n+1)-1. # Perl 5, 24, 21 bytes Thanks to Dom, solutions with 21 bytes $\+=$_--**2while$_}{


or

map$\+=$_**2,1..$_}{  or $\+=$_**2for 1..$_}{


previous were 21 + 1 -p flag, 3 bytes saved thanks to Xcali

$_*=($_+1)*(2*$_+1)/6  and 23 +1 $_=$_*($_+1)*(2*$_+1)/6  or $_=$_**3/3+$_**2/2+$_/6  • You can shorten this by factoring out the$_ and replacing = with *=: Try it online! Oct 17, 2017 at 15:44
• This looks to be 22 bytes (21 + 1 for -p)... But here's one for 21 bytes using a different approach entirely: Try it online! Oct 18, 2017 at 10:13
• good point, there is also $\+=$_--**2while$_}{ Oct 18, 2017 at 10:40 # Befunge, 28 bytes &00pg#v_.@ -1:g00<^g00+*:p00  Works for inputs in the range [0, 128). Due to befunge being entirely stack-based, and yet having limited stack manipulation operations available, the only way to work with three values (sum, partial sum, and counter) is to store a value temporarily by modifying the program itself using the p instruction. Since p assigns a value as ASCII, the stored value wraps around at 128, storing a negative value instead of a positive value. Try it online! # Befunge, 30 bytes & >::*\:v$<^-1_v#<@.
_^#:\+<\


Works for pretty much any input.

Try it online!

# Funky, 3324 19 bytes

-9 bytes thanks to Dennis

-5 bytes thanks to ASCII-only and bugfixes.

f=n=>n?f(n-1)+n*n:0


Try it online!

• 26 bytes Oct 19, 2017 at 5:53

# Python, 38 33 bytes

-5 bytes thanks to Martin Ender

g=lambda x:x*x+g(x-1) if x else 0

Haven't seen any solution with recursion yet, so I thought I'd post this one. It may not be really competitive, but this is my first time posting.

Edit: It would've been -11 bytes thanks to Martin Ender, but I would've ended up with the same answer as Mr. Xcoder's

• Welcome to PPCG! Here are a few ideas: x**2 is x*x, you can avoid the parentheses if you just put the condition on top and avoid the <1 if you switch the true and false branches (because 0 is falsy). However, y if x else 0 can be expressed more concisely as x and y, due to the short-circuiting behaviour of and. So you end up with g=lambda x:x and x*x+g(x-1). :) Nov 1, 2017 at 11:20
• @Martin Ender Thank you for the tips. I am going to simplify the **2 and the <1, but I won't replace the ternary operator with the "and" statement, since it will be the same as Mr. Xcoder's second answer. Nov 1, 2017 at 12:16

# Flurry, 30 bytes

{}{({})[<><<>()>]<[][]>}[<>()]


### Run example

$./flurry -nin -c "{}{({})[<><<>()>]<[][]>}[<>()]" 0 0$ ./flurry -nin -c "{}{({})[<><<>()>]<[][]>}[<>()]" 1
1
$./flurry -nin -c "{}{({})[<><<>()>]<[][]>}[<>()]" 3 14$ ./flurry -nin -c "{}{({})[<><<>()>]<[][]>}[<>()]" 6
91


The computation is done via the stack, but the final result is outputted from the return value.

Given that we can keep track of iteration indexes using the stack height and multiplication is cheap in Flurry, there's really no "clever" alternative that can lead to shorter code.

{}{...}[<>()]  Repeat the lambda n times with the start value of zero
(implicit) push argument x to the stack
({})           Pop and re-push x; return x
[<><<>()>]     succ
<[][]>         (height ∘ height)
In effect, push x and return x + height^2

• "succ" hmmm.... Sep 22, 2020 at 8:14

# QBIC, 13 bytes

[:|p=p+a^2]?p


## Explanation

[:|    FOR a = 1 to <input from cmd line>
p=p+   increment p by
a^2    a squared
]      NEXT
?p     PRINT p


# Proton, 16 bytes

x=>x*(x+++x)*x/6


Try it online!

Straightforward Solution: range+map(x=>x**2)+sum

-2 bytes thanks to Arnauld('s answer)

• @FelipeNardiBatista Oh right, okay. Thanks. Oct 17, 2017 at 12:04

# Pyke, 4 bytes

hLXs


Try it here!

or...

SMXs


# 4, 39 bytes

3.7006110180020100000030301100001195034


Try it online!

How?

3.
7 00        grid = input()
6 11 01     grid = 1
8 00        while grid != 0:
2 01 00 00     grid = grid * grid
0 03 03 01     grid = grid + grid
1 00 00 11     grid = grid - grid
9
5 03        print(grid)
4


# Java 8, 785735 16 bytes

@Arnauld port

i->i*(i+++i)*i/6


# Octave, 14 bytes

@(k)(a=0:k)*a'


Try it online!

*Dot product.

Or

@(k)sumsq(0:k)


Try it online!

f n=n*(n+1)*(2*n+1)/6


Try it online!

Boring, straightforward implementation of the closed-form formula on the OEIS page. Expanding into polynomial form doesn't save any bytes: f n=(2*n^3+3*n^2+n)/6.

• Same byte count: f n=sum$map(^2)[1..n]. Oct 17, 2017 at 13:37 # Tcl, 36 bytes proc S n {expr$n*($n+1)*(2*$n+1)/6}


Try it online!

• Longer approach, that does not work well with zero: tio.run/##K0nO@f@/oCg/… Oct 17, 2017 at 13:20
• Another longer approach, that does not work well with zero: tio.run/##K0nO@f@/oCg/… Oct 17, 2017 at 13:21
• Yet another longer approach, that does not work well with zero: tio.run/##K0nO@f@/oCg/… Oct 17, 2017 at 13:35
• The same approach as the Javascript answer has one byte more: tio.run/##K0nO@f@/oCg/… Tks, @Arnauld Oct 17, 2017 at 14:32

# Ruby, 18 bytes

->n{n*~n*~(n+n)/6}


Using the sama formula as everybody else, saved 1 byte with a double negative multiplication.

Try it online!

## Retina, 20 bytes

.+
$* M!&.+ 1$%_
1


Try it online!

### Explanation

.+
$*  Convert input to unary. M!&.+  Get all overlapping matches of .+ which turns the input into a range of unary numbers from 1 to n. 1$%_


Replace each 1 with the entire line its on, squaring the unary value on each line.

1


Count the number of 1s, summing all lines and converting them back to decimal.

# Pari/GP, 16 bytes

n->(v=[0..n])*v~


Try it online!

# cQuents 0, 3 bytes

;$$ Try it online! Take that, Oasis. ## Explanation ; Mode: Sum - given n, output the sum of the sequence up to n$$   Each term in the sequence equals the index * the index

• does not work for input 0 Oct 18, 2017 at 9:39

# Recursiva, 8 bytes

smBa'Sa'


Try it online!

## Alice, 17 12 bytes

./ O \d2E+.



Try it online!

Prints the sequence indefinitely.

The trailing linefeed is significant.

### Explanation

.   Duplicate the current total. Initially this pushes two zeros onto the
previously empty stack.
/   Switch to Ordinal.
O   Print the current total with a trailing linefeed.
\   Switch back to Cardinal.
d   Push the stack depth, which acts as a counter variable.
2E  Square it.
+   Add it to the current total.
.   Duplicate it to increment the stack depth for the next iteration.


# Pyth, 10 bytes

VhQ=+Z^N2Z


Outputs the first N elements of the sequence.

Try it online!

How?

VhQ=+Z^N2Z              Full program

VhQ                     Loop until Q + 1 is reached
=+Z                  Assign and increment Z by ...
^N2               ... N squared
Z              Implicity prints Z


11 byte alternative.

VhQ aY^N2sY


Try it online!

# Pushy, 6 bytes

R2KeS#


Try it online!

### Explanation

R     Push integers from 1 to implicit n, where n is implicit input
2     Push 2
K     Next command will use the entire stack
e     Pop 2. Raise each of the remaing stack entries to that
S     Sum the entire stack
#     Print as integer


# Jq 1.5, 20 bytes

[range(.+1)|.*.]|add


Input is N. Output is N'th element of sequence. Expanded:

[
range(.+1)            # generate series 0, 1, 2, 3, ... N
| .*.                   # square each term
]                         # collect into an array
| add                     # compute the sum


Try it online!

# Bash, 48 30 bytes

echo "$1*($1+1)*(2*\$1+1)/6"|bc


Try it online!

# K (oK), 9 bytes

Solution:

+/x*x:!1+


Try it online!

Examples:

> +/x*x:!1+4
30
> +/x*x:!1+5
55


Explanation:

Evaluated right-to-left:

+/x*x:!1+ / solution
1+ / add 1 to input, 1+4 => 5
!   / til, !5 => 0 1 2 3 4
x:    / save as variable x
x*      / vectorised multiplication, x*x, 0 1 2 3 4*0 1 2 3 4 => 0 1 3 9 16
+/        / addition over (sum), +/0 1 3 9 16 => 30


# C#(.NET Core), 20 bytes

a=>a*(a+1)*(2*a+1)/6


Try it online!

# ARBLE, 14 13 bytes

-1 bytes thanks to Mr. Xcoder

n*~n*~(n+n)/6


Try it online!

• 13 bytes Oct 17, 2017 at 16:20

# R, 16 bytes

(n=0:scan())%*%n


Try it online!