| Bytes | Lang | Time | Link |
|---|---|---|---|
| 021 | AWK | 171017T144153Z | Nahuel F |
| 016 | Raku | 230612T150957Z | Sean |
| 003 | Nekomata | 230612T035650Z | alephalp |
| 019 | Arturo | 230610T203106Z | chunes |
| 002 | Thunno 2 S | 230610T165318Z | The Thon |
| 003 | Pyt | 221217T152309Z | Kip the |
| 003 | Vyxal | 221011T071814Z | DialFros |
| 003 | MATL | 171017T115552Z | Luis Men |
| 030 | Flurry | 200814T010153Z | Bubbler |
| 003 | Jelly | 181019T221234Z | Mr. Xcod |
| 010 | Milky Way | 171025T125711Z | ovs |
| 022 | Python 2 | 171017T115409Z | Mr. Xcod |
| 033 | Python | 171101T110127Z | pCozmic |
| 013 | Add++ | 171025T211322Z | caird co |
| 019 | Funky | 171019T051749Z | ATaco |
| 028 | Befunge | 171019T195728Z | Brian Gr |
| 010 | TIBasic | 171019T151412Z | Timtech |
| 009 | J | 171018T041219Z | Conor O& |
| 005 | Pyth | 171017T152655Z | Dave |
| 011 | ><> | 171017T141819Z | Emigna |
| 018 | JavaScript ES6 | 171018T133021Z | Shaggy |
| 023 | Hexagony | 171018T090029Z | Martin E |
| 003 | Japt | 171017T121353Z | Erik the |
| 019 | Excel | 171018T111504Z | Wernisch |
| 021 | Perl 5 | 171017T142413Z | Nahuel F |
| 012 | dc | 171018T102055Z | Toby Spe |
| 030 | Common Lisp | 171018T043603Z | Renzo |
| 034 | BrainFlak | 171017T185929Z | Wheat Wi |
| 036 | MaybeLater | 171018T002650Z | ATaco |
| 010 | TIBASIC | 171018T000255Z | Khuldrae |
| 053 | ReRegex | 171017T235611Z | ATaco |
| 040 | Threead | 171017T231705Z | ATaco |
| 010 | TacO | 171017T223730Z | ATaco |
| 149 | Postscript | 171017T222753Z | MongoThe |
| 016 | R | 171017T202718Z | Leaky Nu |
| 013 | ARBLE | 171017T132158Z | ATaco |
| 020 | C#.NET Core | 171017T182326Z | Probably |
| 020 | Haskell | 171017T162927Z | nimi |
| 009 | K oK | 171017T162630Z | mkst |
| 016 | Befunge | 171017T124437Z | Cinaski |
| 015 | Cubix | 171017T141501Z | Giuseppe |
| 030 | Bash | 171017T151214Z | 0x45 |
| 020 | Jq 1.5 | 171017T153815Z | jq170727 |
| 006 | Pushy | 171017T153633Z | Luis Men |
| 010 | Pyth | 171017T151752Z | Ian H. |
| 012 | Alice | 171017T132553Z | Martin E |
| 011 | Labyrinth | 171017T150509Z | Martin E |
| 014 | Julia | 171017T120814Z | Uriel |
| 008 | Recursiva | 171017T141808Z | 0xffcour |
| 003 | cQuents 0 | 171017T134840Z | Stephen |
| 016 | Pari/GP | 171017T134221Z | alephalp |
| 020 | Retina | 171017T133236Z | Martin E |
| 018 | Ruby | 171017T133218Z | G B |
| 036 | BrainFlak | 171017T132107Z | Riley |
| 009 | CJam | 171017T132832Z | Martin E |
| 016 | JavaScript ES6 | 171017T120722Z | Arnauld |
| 017 | R | 171017T131452Z | Rudier |
| 036 | Tcl | 171017T130357Z | sergiol |
| 021 | Haskell | 171017T125612Z | user4594 |
| 005 | APL Dyalog | 171017T120330Z | Uriel |
| 003 | Ohm v2 | 171017T120055Z | Cinaski |
| 014 | Octave | 171017T122943Z | rahnema1 |
| 016 | Java 8 | 171017T121835Z | Roberto |
| 003 | Actually | 171017T123149Z | user4594 |
| 039 | 4 | 171017T122328Z | Uriel |
| 004 | Pyke | 171017T122015Z | Mr. Xcod |
| 003 | Gaia | 171017T121714Z | Mr. Xcod |
| 005 | Brachylog | 171017T121316Z | Fatalize |
| 014 | Wolfram Language Mathematica | 171017T120128Z | totallyh |
| 004 | Oasis | 171017T120413Z | Emigna |
| 016 | Proton | 171017T115918Z | hyperneu |
| 003 | Neim | 171017T120957Z | Okx |
| 003 | 05AB1E | 171017T120319Z | Emigna |
| 010 | CJam | 171017T120542Z | Luis Men |
| 003 | Husk | 171017T120519Z | Mr. Xcod |
| 013 | QBIC | 171017T120412Z | steenber |
| 046 | BrainFlak | 171017T120226Z | hyperneu |
| 003 | Jelly | 171017T115551Z | hyperneu |
Raku, 16 bytes
[\+] {$++²}...*
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.
Nekomata, 3 bytes
R:∙
R:∙
R Range
: Duplicate
∙ Dot product
Because there isn't a square built-in yet.
Thunno 2 S, 2 bytes
R²
Explanation
R² # Implicit input
R # Range of input
² # Square each
# Sum the list
# Implicit output
Pyt, 3 bytes
ř²Ʃ
ř²Ʃ
implicit input
ř creates array of 1 to n
² squares each element
Ʃ sums the list
implicit print
MATL, 3 bytes
:Us
... or them?
Explanation
:Us
: % Implicit input n. Push range [1 2 ... n]
U % Square, element-wise
s % Sum of array. Implicit display
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
Milky Way, 10 bytes
'L§{:*}G!
Explanation:
' push input to stack
L push inclusive range
${ } map over every element ...
:* ... duplicate and multiply (square it)
G sum of list
! output
Python 2, 22 bytes
lambda n:n*~n*~(n*2)/6
This uses the closed-form formula n * (n+1) * (2*n+1) / 6. The code performs the following operations:
Multiplies n by (
n*):- The bitwise complement of n (
~n), which essentially means -1-n. - And by the bitwise complement of 2n (
*~(n*2)), which means -1-2n.
- The bitwise complement of n (
Divides by 6 (
/6).
Python 2, 27 bytes
f=lambda n:n and f(n-1)+n*n
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
Add++, 13 bytes
D,f,@,RdBcB*s
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
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
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.
Befunge, 30 bytes
& >::*\:v
$<^-1_v#<@.
_^#:\+<\
Works for pretty much any input.
TI-Basic, 10 bytes
sum(seq(OO,O,0,Ans
sp00ky
J, 10 9 bytes
4%~3!2*>:
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.
Pyth, 7 5 bytes thanks to Steven H
s^R2h
Explanation:
s^R2h Full program - inputs from stdin and outputs to stdout
s output the sum of
h range(input), with
^R2 each element squared
My first solution
sm*ddUh
Explanation:
sm*ddUh Full program - inputs from stdin and outputs to stdout
s sum of
m Uh each d in range(input)
*dd squared
><>, 15 13 11 bytes
Saved 2 bytes thanks to Not a tree
0:n:l1-:*+!
Outputs the sequence indefinitely.
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>
Hexagony, 23 bytes
?'+)=:!@/*"*'6/{=+'+}/{
Explanation
Unfolded:
? ' + )
= : ! @ /
* " * ' 6 /
{ = + ' + } /
{ . . . . .
. . . . .
. . . .
This is really just a linear program with the / used for some redirection. The linear code is:
?'+){=+'+}*"*'6{=:!@
Which computes n (n+1) (2n+1) / 6. It uses the following memory edges:
Where the memory point (MP) starts on the edge labelled n, pointing north.
? Read input into edge labelled 'n'.
' Move MP backwards onto edge labelled 'n+1'.
+ Copy 'n' into 'n+1'.
) Increment the value (so that it actually stores the value n+1).
{= Move MP forwards onto edge labelled 'temp' and turn around to face
edges 'n' and 'n+1'.
+ Add 'n' and 'n+1' into edge 'temp', so that it stores the value 2n+1.
' Move MP backwards onto edge labelled '2n+1'.
+ Copy the value 2n+1 into this edge.
} Move MP forwards onto 'temp' again.
* Multiply 'n' and 'n+1' into edge 'temp', so that it stores the value
n(n+1).
" Move MP backwards onto edge labelled 'product'.
* Multiply 'temp' and '2n+1' into edge 'product', so that it stores the
value n(n+1)(2n+1).
' Move MP backwards onto edge labelled '6'.
6 Store an actual 6 there.
{= Move MP forwards onto edge labelled 'result' and turn around, so that
the MP faces edges 'product' and '6'.
: Divide 'product' by '6' into 'result', so that it stores the value
n(n+1)(2n+1)/6, i.e. the actual result.
! Print the result.
@ Terminate the program.
In theory it might be possible to fit this program into side-length 3, because the / aren't needed for the computation, the : can be reused to terminate the program, and some of the '"=+*{ might be reusable as well, bringing the number of required commands below 19 (the maximum for side-length 3). I doubt it's possible to find such a solution by hand though, if one exists at all.
Excel, 19 bytes
=A1^3/3+A1^2/2+A1/6
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
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.
Brain-Flak, 34 bytes
({(({}[()])()){({}[()])({})}{}}{})
How does it work?
Initially I had the same idea as Riley1 but it felt wrong to use a zeroer. I then realized that
{({}[()])({})}{}
Calculates n2 - n.
Why? Well we know
{({})({}[()])}{}
Calculates n2 and loops n times. That means if we switch the order of the two pushes we go from increasing the sum by n + (n-1) each time to increasing the sum by (n-1) + (n-1) each time. This will decrease the result by one per loop, thus making our result n2 - n. At the top level this -n cancels with the n generated by the push that we were zeroing alleviating the need for a zeroer and saving us two bytes.
Brain-Flak, 36 bytes
({({})(({}[()])){({})({}[()])}{}}{})
Here is another solution, its not as golfy but it's pretty strange so I thought I would leave it as a challenge to figure out how it works.
If you are not into Brain-Flak but you still want the challenge here it is as a summation.
1: I came up with my solution before I looked at the answers here. So no plagiarism here.
TI-BASIC, 10 bytes
sum(seq(X²,X,0,Ans
Quite simple. The sum of the sequence of the first Ans squares.
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>
Threead, 40 bytes
I[ -]_
_^>1
_2 _r
[<
lo
]
+
De-sliced
This is actually 4 bytes larger, due to whitespace.
I[ -]_ lo
_^>1 [< ]
_2 _r +
Process.
- Read an integer into the first value
- While the first value is non-zero.
- Blank the third value, push 2 onto it.
- Blank the second value, then calculate the first value to the power of the third, (i^2)
- Move the second value to the right and place a 1 onto it.
- Put the value of the first value onto the third value.
- Calculate the third value minus the second value (i-1)
- Now the second tape contains the squares of all numbers from inp to 1 squared.
- Blank the first value
- While the second value is non-zero
- Move the second tape to the left.
- Add the first value to the second value into the third value.
- Move the third value into the first value
- Now, all the i^2 values are summed together, output the first value.
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
K (oK), 9 bytes
Solution:
+/x*x:!1+
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
Befunge, 16 bytes
&::2*1+*\1+*6/.@
Using the closed-form formula n*(n+1)*(2n+1)/6.
Befunge, 38 bytes
v>::*\1-:v0+_$.@
&^ < _\:^
>:#^_.@
Using a loop.
Cubix, 15 bytes
Iu):^\+*p*6u@O,
My code is a bit sad ):
Computes n*(n+1)*(2n+1)/6
I u
) :
^ \ + * p * 6 u
@ O , . . . . .
. .
. .
^Iu : read in input, u-turn
: stack n
:)\ : dup, increment, go right..oh, hey, it cheered up!
: stack: n, n+1
+ : sum
: stack: n, n+1, 2*n+1
* : multiply
: stack: n, n+1, 2*n+1, (n+1)*(2*n+1)
p : move bottom of stack to top
: stack: n+1, 2*n+1, (n+1)*(2*n+1), n
* : multiply
6 : push 6
u : right u-turn
, : divide
O : output
@ : terminate
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
Pushy, 6 bytes
R2KeS#
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
Pyth, 10 bytes
VhQ=+Z^N2Z
Outputs the first N elements of the sequence.
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
Alice, 17 12 bytes
./ O \d2E+.
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.
Labyrinth, 11 bytes
:!\
+ :
*:#
Prints the sequence indefinitely.
Explanation
The instruction pointer just keeps running around the square of code over and over:
:!\ Duplicate the last result (initially zero), print it and a linefeed.
: Duplicate the result again, which increases the stack depth.
# Push the stack depth (used as a counter variable).
:* Square it.
+ Add it to the running total.
Julia, 16 14 bytes
2 bytes saved thanks to @MartinEnder
!n=(x=1:n)⋅x
How?
(x=1:n) creates a range of 1 to n and assign to x, ⋅ dot product with x.
cQuents 0, 3 bytes
;$$
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
Retina, 20 bytes
.+
$*
M!&`.+
1
$%_
1
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.
Ruby, 18 bytes
->n{n*~n*~(n+n)/6}
Using the sama formula as everybody else, saved 1 byte with a double negative multiplication.
Brain-Flak, 36 bytes
({<(({}[()])())>{({})({}[()])}{}}{})
# Main algorithm
( ) # Push the sum of:
{({})({}[()])}{} # The square of:
{ } # 0 to i
# Stuff for the loop
<(({}[()])())> # Push i-1, i without counting it in the sum
{} # Pop the counter (0)
CJam, 9 bytes
ri),_.*:+
Explanation
ri e# Read input and convert to integer N.
), e# Get range [0 1 2 ... N].
_ e# Duplicate.
.* e# Pairwise products, giving [0 1 4 ... N^2].
:+ e# Sum.
Alternatively:
ri),2f#:+
This squares each element by mapping 2# instead of using pairwise products. And just for fun another alternative which gets inaccurate for large inputs because it uses floating-point arithmetic:
ri),:mh2#
JavaScript (ES6), 16 bytes
n=>n*(n+++n)*n/6
Demo
let f =
n=>n*(n+++n)*n/6
for(n = 0; n < 10; n++) {
console.log('a(' + n + ') = ' + f(n))
}
How?
The expression n+++n is parsed as n++ + n (1). Not that it really matters because n + ++n would also work in this case.
Therefore:
n*(n+++n)*n/6 =
n * (n + (n + 1)) * (n + 1) / 6 =
n * (2 * n + 1) * (n + 1) / 6
which evaluates to sum(k=0...n)(k²).
(1) This can be verified by doing n='2';console.log(n+++n) which gives the integer 5, whereas n + ++n would give the string '23'.
R, 17 bytes
sum((0:scan())^2)
Pretty straightforward, it takes advantage from the fact that ^ (exponentiation) is vectorized in R.
Haskell, 21 bytes
f n=n*(n+1)*(2*n+1)/6
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.
APL (Dyalog), 7 5 bytes
2 bytes saved thanks to @Mego
+.×⍨⍳
How?
⍳ - range
+.× - dot product
⍨ - with itself
Java 8, 78 57 35 16 bytes
@Arnauld port
i->i*(i+++i)*i/6
Actually, 3 bytes
R;*
Takes N as input, and outputs the Nth element in the sequence.
Explanation:
R;*
R range(1, N+1) ([1, 2, ..., N])
;* dot product with self
4, 39 bytes
3.7006110180020100000030301100001195034
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
Proton, 16 bytes
x=>x*(x+++x)*x/6
Straightforward Solution: range+map(x=>x**2)+sum
-2 bytes thanks to Arnauld('s answer)
Neim, 3 bytes
𝐈ᛦ𝐬
This could've been a challenge to show off Neim's polygonal number builtins, but apparently not.
CJam, 10 bytes
ri),{_*+}*
ri e# Input integer n
) e# Add 1
, e# Range [0 1 ... n]
{ }* e# Fold (reduce)
_ e# Duplicate
* e# Multiply
+ e# Add
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
