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

# TacO, 10 bytes

@i i
+%+*i


Try it online!

I[     -]_
_^>1
_2 _r

[<

lo
]
+


Try it online!

## De-sliced

This is actually 4 bytes larger, due to whitespace.

I[     -]_   lo
_^>1   [< ]
_2 _r     +


## Process.

1. Read an integer into the first value
2. While the first value is non-zero.
1. Blank the third value, push 2 onto it.
2. Blank the second value, then calculate the first value to the power of the third, (i^2)
3. Move the second value to the right and place a 1 onto it.
4. Put the value of the first value onto the third value.
5. Calculate the third value minus the second value (i-1)
3. Now the second tape contains the squares of all numbers from inp to 1 squared.
4. Blank the first value
5. While the second value is non-zero
1. Move the second tape to the left.
2. Add the first value to the second value into the third value.
3. Move the third value into the first value
6. Now, all the i^2 values are summed together, output the first value.

# ReRegex, 53 bytes

#import math
^k([1-9]\d*)/k($1-1)+$1*$1/k0+/0/k#input  ## Explained #import math # Import the math library. ^k([1-9]\d*)/k($1-1)+$1*$1/ # Search the buffer for "k" followed by a non-zero number. Replace it with "k<n-1>+n*n"
k0+/0/                      # Replace k0 with just 0
k#input                     # Default the buffer to k<input>


Try it online!

## TI-BASIC, 10 bytes

sum(seq(X²,X,0,Ans


Quite simple. The sum of the sequence of the first Ans squares.

# MaybeLater, 36 bytes

n=(readn)/1
write(n*(n+1)*(1+n*2)/6)


Using the closed form formula.

Try it online!

• Surely you could have posted two different approaches in the same answer? Oct 18 '17 at 4:12

# Common Lisp, 30 bytes

(loop as i to(read)sum(* i i))


Try it online!

# JavaScript (ES6), 18 bytes

f=n=>n&&n*n--+f(n)


## Try it

o.innerText=(
f=n=>n&&n*n--+f(n)
)(i.value=8);oninput=_=>o.innerText=f(+i.value)
<input id=i type=number><pre id=o>

# TI-Basic, 10 bytes

sum(seq(OO,O,0,Ans


sp00ky

D,f,@,RdBcB*s


Try it online!

D,f,@,   - Create a monadic function called f. Argument n (example: 5)
R  - Generate range;   STACK = [[1 2 3 4 5]]
d  - Duplicate;        STACK = [[1 2 3 4 5] [1 2 3 4 5]]
Bc - Push the columns; STACK = [[1 1] [2 2] [3 3] [4 4] [5 5]]
B* - Product of each;  STACK = [1 4 9 16 25]
s  - Push the sum;     STACK = [1 4 9 16 25 55]
- Implicitly return 55


# Milky Way, 10 bytes

'L§{:*}G!


Try it online!

## Explanation:

'          push input to stack
L         push inclusive range
\${  }    map over every element ...
:*     ... duplicate and multiply (square it)
G   sum of list
!  output


# Jelly, 3 bytes

Rḋ


Try it online!

# Postscript, 149 bytes

/d {def}def
/a {
0 i l 1 0 l
i 2 ge {/i i 1 sub d a}if
} d
/l {rlineto}d
/m {moveto}d
/b {/j exch d /i j d 0 0 m j a j 0 lineto closepath fill}d
5 b


I'm a beginner in programming. I just do that for fun and I'm thankful for any help.

I tried in JavaScript:

var x = 9;

var y = 0;

var z;

for(var i=0; i<=x; i++){z = i*i; y += z;}

console.log(y)
`
• Input cannot be stored in a variable. It must be given by the user or taken as a function parameter. See this meta post Oct 17 '17 at 16:51
• Welcome to PPCG! The point of this challenge is to answer in as little code as possible. It looks like you've made a good start, but you can definitely make it shorter, such as by removing white space. In addition, please consider what @mbomb007 has said about taking input. Oct 17 '17 at 16:51