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

73 Answers 73

1
\$\begingroup\$

Gaia, 3 bytes

s¦Σ

Try it online!

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

awk, 25 bytes

{print$1^3/3+$1^2/2+$1/6}
\$\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\$
    – Xcali
    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\$ Oct 18, 2017 at 10:13
  • 1
    \$\begingroup\$ good point, there is also $\+=$_--**2while$_}{ \$\endgroup\$ 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\$
    – Dennis
    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\$ 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\$
    – pCozmic
    Nov 1, 2017 at 12:16
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\$
    – Razetime
    Sep 22, 2020 at 8:14
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\$
1
  • \$\begingroup\$ @FelipeNardiBatista Oh right, okay. Thanks. \$\endgroup\$
    – hyper-neutrino
    Oct 17, 2017 at 12:04
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\$
    – Laikoni
    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\$
4
0
\$\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\$
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\$ 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\$
0
\$\begingroup\$

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

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!

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

Bash, 48 30 bytes

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

Try it online!

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

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

C#(.NET Core), 20 bytes

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

Try it online!

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

ARBLE, 14 13 bytes

-1 bytes thanks to Mr. Xcoder

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

Try it online!

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

R, 16 bytes

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

Try it online!

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.