# 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). – James Oct 17 '17 at 16:31
• May we begin the sequence at f(1) = 1? – Emigna Oct 18 '17 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 – Felipe Nardi Batista Oct 18 '17 at 9:44
• The f(0) = 0 requirement ruined a few of my solutions :( – ATaco Oct 19 '17 at 5:19

{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! – Xcali Oct 17 '17 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! – Dom Hastings Oct 18 '17 at 10:13
• good point, there is also $\+=$_--**2while$_}{ – Nahuel Fouilleul Oct 18 '17 at 10:40 # J, 10 9 bytes 4%~3!2*>:  Try it online! Saved 1 byte using an explicit formula, thanks to miles! ## J, 10 bytes 1#.]*:@-i.  J has a range function, but this gives us number from 0 to N-1. To remedy this, we can just take the argument and subtract the range from it, giving us a range from N to 1. This is done with ] -i.. The rest of the code simply square this list argument (*:@) and then sums it (1#.). ## Other contenders 11 bytes: 1#.*:@i.@>: 13 bytes: 1#.[:,@:*/~i. • 4%~3!2*>: saves a byte – miles Oct 19 '17 at 11:45 # 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 – Dennis Oct 19 '17 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). :) – Martin Ender Nov 1 '17 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. – pCozmic Nov 1 '17 at 12:16

# 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. – HyperNeutrino Oct 17 '17 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[0] = input()
6 11 01     grid[11] = 1
8 00        while grid[0] != 0:
2 01 00 00     grid[1] = grid[0] * grid[0]
0 03 03 01     grid[3] = grid[3] + grid[1]
1 00 00 11     grid[0] = grid[0] - grid[11]
9
5 03        print(grid[3])
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]. – Laikoni Oct 17 '17 at 13:37 # Tcl, 36 bytes proc S n {expr$n*($n+1)*(2*$n+1)/6}


Try it online!

# 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 – Felipe Nardi Batista Oct 18 '17 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!

# R, 16 bytes

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


Try it online!

# TacO, 10 bytes

@i i
+%+*i


Try it online!