31
\$\begingroup\$

A000330 - OEIS

Task

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.
\$\endgroup\$
4
  • 3
    \$\begingroup\$ Fun observation from OEIS: This sequence contains exactly two perfect squares: f(1) == 1 * 1 (1), and f(24) == 70 * 70 (4900). \$\endgroup\$ Commented Oct 17, 2017 at 16:31
  • \$\begingroup\$ May we begin the sequence at f(1) = 1? \$\endgroup\$ Commented Oct 18, 2017 at 6:10
  • \$\begingroup\$ @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 \$\endgroup\$ Commented Oct 18, 2017 at 9:44
  • \$\begingroup\$ The f(0) = 0 requirement ruined a few of my solutions :( \$\endgroup\$ Commented Oct 19, 2017 at 5:19

78 Answers 78

2
\$\begingroup\$

Raku, 16 bytes

[\+] {$++²}...* 

Try it online!

This is an expression for the lazy infinite sequence of numbers.

  • { $++² } ... * is the infinite sequence of square numbers. $ is an anonymous state variable whose postincremented value is squared to obtain each new element.
  • [\+] is the sequence of partial sums of that sequence.
\$\endgroup\$
1
\$\begingroup\$

Neim, 3 bytes

𝐈ᛦ𝐬 

This could've been a challenge to show off Neim's polygonal number builtins, but apparently not.

Try it online!

\$\endgroup\$
1
\$\begingroup\$

Oasis, 4 bytes

nk+0 

Try it online!

Explanation

 0 # a(0) = 0 nk+ # a(n) = a(n-1)+n^2 
\$\endgroup\$
1
\$\begingroup\$

Gaia, 3 bytes

s¦Σ 

Try it online!

\$\endgroup\$
1
\$\begingroup\$

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!

\$\endgroup\$
1
\$\begingroup\$

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.

\$\endgroup\$
1
\$\begingroup\$

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 
\$\endgroup\$
3
  • \$\begingroup\$ You can shorten this by factoring out the $_ and replacing = with *=: Try it online! \$\endgroup\$ Commented Oct 17, 2017 at 15:44
  • \$\begingroup\$ 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! \$\endgroup\$ Commented Oct 18, 2017 at 10:13
  • 1
    \$\begingroup\$ good point, there is also $\+=$_--**2while$_}{ \$\endgroup\$ Commented Oct 18, 2017 at 10:40
1
\$\begingroup\$

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!

\$\endgroup\$
1
\$\begingroup\$

Funky, 33 24 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!

\$\endgroup\$
1
  • \$\begingroup\$ 26 bytes \$\endgroup\$ Commented Oct 19, 2017 at 5:53
1
\$\begingroup\$

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

\$\endgroup\$
2
  • \$\begingroup\$ 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). :) \$\endgroup\$ Commented Nov 1, 2017 at 11:20
  • \$\begingroup\$ @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. \$\endgroup\$ Commented Nov 1, 2017 at 12:16
1
\$\begingroup\$

Jelly, 3 bytes

Rḋ` 

Try it online!

\$\endgroup\$
1
\$\begingroup\$

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 
\$\endgroup\$
1
  • \$\begingroup\$ "succ" hmmm.... \$\endgroup\$ Commented Sep 22, 2020 at 8:14
1
\$\begingroup\$

Vyxal, 3 bytes

ɾ²∑ 

Try it Online!

Explained

ɾ²∑ ɾ # range 1 inclusive [1...n] ² # square ∑ # sum 
\$\endgroup\$
1
\$\begingroup\$

Pyt, 3 bytes

ř²Ʃ 

Try it online!

ř²Ʃ implicit input ř creates array of 1 to n ² squares each element Ʃ sums the list implicit print 
\$\endgroup\$
1
\$\begingroup\$

Arturo, 19 bytes

$=>[∑map&=>[&^2]] 

Try it!

\$\endgroup\$
1
\$\begingroup\$

AWK, 25 23 bytes

Thanks to xrs

1,$0=$1^3/3+$1^2/2+$1/6 

Try it online!

\$\endgroup\$
1
  • \$\begingroup\$ I like the math. 1,$0=$1^3/3+$1^2/2+$1/6 shaves a couple bytes. \$\endgroup\$ Commented Mar 31 at 17:53
0
\$\begingroup\$

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 
\$\endgroup\$
0
\$\begingroup\$

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)

\$\endgroup\$
0
0
\$\begingroup\$

Pyke, 4 bytes

hLXs 

Try it here!

or...

SMXs 
\$\endgroup\$
0
\$\begingroup\$

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 
\$\endgroup\$
0
\$\begingroup\$

Java 8, 78 57 35 16 bytes

@Arnauld port

i->i*(i+++i)*i/6 
\$\endgroup\$
0
0
\$\begingroup\$

Octave, 14 bytes

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

Try it online!

*Dot product.

Or

@(k)sumsq(0:k) 

Try it online!

\$\endgroup\$
0
\$\begingroup\$

Haskell, 21 bytes

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.

\$\endgroup\$
1
  • 1
    \$\begingroup\$ Same byte count: f n=sum$map(^2)[1..n]. \$\endgroup\$ Commented Oct 17, 2017 at 13:37
0
\$\begingroup\$

Tcl, 36 bytes

proc S n {expr $n*($n+1)*(2*$n+1)/6} 

Try it online!

\$\endgroup\$
5
  • \$\begingroup\$ Longer approach, that does not work well with zero: tio.run/##K0nO@f@/oCg/… \$\endgroup\$ Commented Oct 17, 2017 at 13:20
  • \$\begingroup\$ Another longer approach, that does not work well with zero: tio.run/##K0nO@f@/oCg/… \$\endgroup\$ Commented Oct 17, 2017 at 13:21
  • \$\begingroup\$ Yet another longer approach, that does not work well with zero: tio.run/##K0nO@f@/oCg/… \$\endgroup\$ Commented Oct 17, 2017 at 13:35
  • \$\begingroup\$ The same approach as the Javascript answer has one byte more: tio.run/##K0nO@f@/oCg/… Tks, @Arnauld \$\endgroup\$ Commented Oct 17, 2017 at 14:32
  • \$\begingroup\$ Alternative approach, 50 bytes. ##### Requires Tcl >= 8.7 to allow use of lseq. 50 bytes: proc S n {expr [lmap i [lseq 0 $n] {list +$i**2}]} \$\endgroup\$ Commented Nov 13 at 22:20
0
\$\begingroup\$

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.

\$\endgroup\$
0
\$\begingroup\$

Pari/GP, 16 bytes

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

Try it online!

\$\endgroup\$
0
\$\begingroup\$

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 
\$\endgroup\$
1
  • \$\begingroup\$ does not work for input 0 \$\endgroup\$ Commented Oct 18, 2017 at 9:39
0
\$\begingroup\$

Recursiva, 8 bytes

smBa'Sa' 

Try it online!

\$\endgroup\$
0
\$\begingroup\$

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. 
\$\endgroup\$
0
\$\begingroup\$

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!

\$\endgroup\$

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.