| Bytes | Lang | Time | Link |
|---|---|---|---|
| 019 | Perl 5 MListUtil=sum p | 240123T184230Z | Xcali |
| 015 | Trilangle | 230516T114728Z | Bbrk24 |
| 009 | Uiua SBCS | 240104T053049Z | chunes |
| 021 | AWK | 240104T051807Z | Bubbler |
| 026 | Ruby | 230820T214424Z | kait0u |
| 020 | Desmos | 230516T161332Z | Aiden Ch |
| 015 | K ngn/k | 230820T064511Z | doug |
| 110 | brainf**k | 230718T133338Z | Shiran Y |
| 038 | Racket + math | 230717T234913Z | Ed The & |
| 020 | AArch64 machine code | 230712T094714Z | landfill |
| 018 | Moonscript/Yuescript | 230713T110814Z | UrNightm |
| 019 | Bash/Zsh | 230712T061747Z | UrNightm |
| 012 | Juby | 230618T040522Z | Jordan |
| 023 | AWK | 230608T070159Z | Marius_C |
| 025 | Scala | 230525T055951Z | 138 Aspe |
| 009 | K ngn/k | 230524T142924Z | coltim |
| 004 | Neim | 230524T132359Z | LiefdeWe |
| 013 | ARBLE | 230524T032731Z | ATaco |
| 002 | Husk | 230522T101916Z | Dominic |
| 019 | R | 230518T044416Z | Jonathan |
| 007 | minigolf | 230521T053132Z | user1180 |
| 018 | SAS IML | 230520T204505Z | Bartosz |
| 006 | BQN | 230516T152636Z | DLosc |
| 015 | Wolfram Language Mathematica | 230516T113000Z | ZaMoC |
| 004 | Pyth | 230519T203222Z | CursorCo |
| 002 | Pyt | 230519T145242Z | Kip the |
| 000 | Vyxal Rss | 230516T072255Z | lyxal |
| 021 | Factor + math.unicode | 230517T185257Z | chunes |
| 018 | Arturo | 230517T183212Z | chunes |
| 004 | Jelly | 230517T174729Z | xigoi |
| 017 | Haskell | 230516T172518Z | xnor |
| 036 | Nim | 230517T170528Z | xigoi |
| 032 | BrainFlak | 230517T164258Z | DJMcMayh |
| 037 | Go | 230516T132421Z | bigyihsu |
| 054 | Rockstar | 230516T111825Z | Shaggy |
| nan | Binary Lambda Calculus | 230517T083425Z | Bubbler |
| 016 | Julia | 230516T205929Z | Kirill L |
| 013 | x8616 machine code | 230516T202139Z | 640KB |
| 021 | Haskell | 230516T172243Z | Roman Cz |
| 019 | Retina 0.8.2 | 230516T165313Z | Neil |
| 022 | C gcc | 230516T154710Z | c-- |
| 023 | Python | 230516T093425Z | solid.py |
| 024 | Nibbles | 230516T151731Z | DLosc |
| 004 | Vyxal | 230516T151309Z | DLosc |
| 022 | SAS 4GL | 230516T140738Z | Bartosz |
| 016 | PARI/GP | 230516T074614Z | alephalp |
| 024 | Lua | 230516T113041Z | bluswimm |
| 025 | PHP | 230516T105331Z | Shaggy |
| 004 | Thunno 2 | 230516T104935Z | noodle m |
| 017 | Java | 230516T095420Z | Kevin Cr |
| 017 | JavaScript Node.js | 230516T093451Z | tsh |
| 004 | MathGolf | 230516T094201Z | Kevin Cr |
| 004 | 05AB1E | 230516T092356Z | Kevin Cr |
| 010 | Charcoal | 230516T090837Z | Neil |
| 005 | Japt x | 230516T085943Z | Shaggy |
| 013 | ><> Fish | 230516T071926Z | mousetai |
| 024 | Excel | 230516T070635Z | Jos Wool |
| 006 | Ly | 230516T071723Z | cnamejj |
| 006 | APL Dyalog Unicode | 230516T065711Z | Adá |
| 065 | C | 230516T065230Z | Breadlea |
Trilangle, 15 bytes
?22)d**2'd')2p@
Try it on the online interpreter!
Just the code for "sum range" twice.
AWK, 21 bytes
$0*=$0+1,$0*=($0+2)/8
Given a positive integer as input, the following happens in sequence:
- The pattern
$0*=$0+1is tested.$0initially contains the input numbern, and is overwritten ton^2+n. Since this is nonzero, the pattern is matched, and a "range match" starts. - The pattern
$0*=($0+2)/8is tested.$0is overwritten to(n^2+n)(n^2+n+2)/8. Since this is nonzero, the pattern is also matched, and the "range match" ends immediately. - The default action is performed, which is to print
$0.
Ruby, 26 bytes
The following is probably the most direct approach to the problem given Ruby's basic methods.
->(n){(1..(1..n).sum).sum}
Another idea: Let T(n) be the n-th triangular number. Our answer is T(T(n)) - we're interested in the T(n)-th triangular number.
brainf**k, 110 bytes
,[>+>+>+<<<-]>>>[-[>+>+<<-]>>[-<<+>>]<<[-<+>]>[-<+>]<]<[>+>+>+<<<-]>>>[-[>+>+<<-]>>[-<<+>>]<<[-<+>]>[-<+>]<]<.
Probably not the shortest implementation but I'm too lazy to make this shorter lol...
Like most other b****fuck programs, it doesn't output the answer. Instead, it prints the ascii character corresponding to the numeric answer.
Check this out to see the running process.
Racket + math, 38 bytes
(sum(range(+(sum(range(+(read)1)))1)))
Although it's verbose and a bit slow, it's shorter than placing in the actual equation and/or using apply + instead of sum, and it doesn't cheat by using triangle-number:
43 bytes + faster
((λ(f)(f(f(read))))(λ(n)(/(*(+ n 1)n)2)))46 bytes, no
mathimport(apply +(range(+(apply +(range(+(read)1)))1)))36 bytes, using
triangle-numberfrommath, it's fast and -2 bytes, but might be somewhat cheating?((λ(t)(t(t(read))))triangle-number)
AArch64 machine code, 20 bytes
00: 9b000000 madd x0, x0, x0, x0 // x = x * x + x
04: d341fc00 lsr x0, x0, #1 // x = x / 2
08: 9b000000 madd x0, x0, x0, x0 // x = x * x + x
0c: d341fc00 lsr x0, x0, #1 // x = x / 2
10: d65f03c0 ret
simple :)
use it as a function uint64_t f(uint64_t x). will overflow on inputs > 92681.
you could also use it as main (not _start unless you replace the ret with an exit syscall) and its input will be the argument count including the program name.
AWK, 23 bytes
$0=int((1+$0^2+$0)^2/8)
This will also work if the input is 0 (28 thank to @Dominic van Essen 24 bytes):
$0=int((1+$0^2+$0)^2/8)a
Scala, 25 bytes
Saved 1 byte(s) thanks to the comment of @Jo King ♦
What's it?
Accroding to A002817, doubly triangular numbers are revealed in the sums of row sums of Floyd's triangle.
1, 1+5, 1+5+15, ...
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
Golfed version. Try it online!
n=>(1 to(1 to n).sum).sum
Ungolfed version. Try it online!
object Main {
def main(args: Array[String]): Unit = {
def t(n: Int): Int = (1 to (1 to n).sum).sum
println((0 to 9).map(t).map(_.toString).mkString(" "))
}
}
K (ngn/k), 9 bytes
2{x+/!x}/
2{...}/set up a monadic do-reduce, running the code in{...}twice!xgenerate0..x-1x+/set up a plus-reduce (i.e. sum) seeded with the inputx
ARBLE, 13 bytes
(n^2-~n)^2//8
Direct port of bluswimmer's port of tsh's answer, utilising ARBLE's more terse extensions.
Husk, 2 bytes
‼Σ
‼ # Apply function twice:
Σ # Triangular number
(the more-boring ΣΣ is also 2 bytes...)
minigolf, 7 bytes
2,,n;+_
Explanation
2, _ Repeat 2 times:
,n; Generate a [1..n] range
+ Sum it
Implicit output
SAS IML, 18 bytes
The code:
o=sum(1:sum(1:n));
The code plus "environment" and nice printout:
proc IML;
do n= 1 to 10;
o=sum(1:sum(1:n));
print n " => " o;
end;
quit;
The log:
1 proc IML;
NOTE: IML Ready
2 do n= 1 to 10;
3 o=sum(1:sum(1:n));
4 print n " => " o;
5 end;
6 quit;
NOTE: Exiting IML.
NOTE: PROCEDURE IML used (Total process time):
real time 0.01 seconds
cpu time 0.01 seconds
Output:
Acknowledgement:
Thank you to Mr. Roman Czyborra for the Haskel inspiration!
BQN, 9 6 bytes
-3 bytes thanks to att
+´⟜↕⍟2
Anonymous tacit function that takes a number and returns a number. Try it at BQN online!
Explanation
Same idea as Adám's APL answer, slightly complicated by the fact that BQN's ranges are 0-based.
Call the argument N:
+´⟜↕⍟2
↕ Range (0 to N-1)
+´ Fold on addition...
⟜ ... with a starting value of N
⍟2 Apply that function twice
Wolfram Language (Mathematica), 15 bytes
#@*#&@Tr@*Range
-1 byte from @att
Wolfram Language (Mathematica), 16 bytes
(f=Tr@*Range)@*f
Pyth, 4 bytes
sSsS
Explanation
sSsSQ # implicitly add Q
# implicitly assign Q = eval(input())
SQ # range 1 to Q
s # sum
S # range 1 to result
s # sum
Extra nice since when pronounced out loud it is the sound a python makes.
Vyxal Rss, 0 bytes
That's right - with the power of subscription models, this task can be done in 0 bytes!
but how does Really Simple Syndication help you sum a range twice?
Well technically speaking it doesn't - of course there's no rss feed involved. What's happening is that the R flag coerces numbers to ranges when an iterable is needed, and the two s flags sum the top of the stack at the end of execution. Due to the way the flag handler is implemented, duplicate flags apply the flag effect multiple times.
Haskell, 17 bytes
g.g
g n=sum[1..n]
Just implementing the definition seems to be shorter than arithmetical expressions such as
21 bytes
f n=((n^2+n+1)^2-1)/8
With floats:
17 bytes
g.g
g n=n*(n+1)/2
Nim, 36 bytes
func a[I](n:I):I=(n*n+n)*(n*n+n+2)/8
Takes and returns a float (so I can use / instead of div).
Brain-Flak, 32 bytes
({({}[()])()}{})
({({}[()])()}{})
(The newline isn't needed, but is included because it makes the answer pretty)
Polygonal numbers are one of the things that brain-flak uniquely excels at. This is just the code that calculates the N-th triangular number, repeated twice.
# Push (the sum of...)
(
# While the top of stack is not 0...
{
# Decrement the top of the stack by 1
# This adds n-1 to the running counter
({}[()])
# Plus 1 (to the running counter)
()
}
# Pop the stack (which should contain a 0 now)
{}
)
Rockstar, 68 54 bytes
listen to N
cast N
let N be*N+1
let X be N+2
say N*X/8
Try it here (Code will need to be pasted in)
Saved 12 byes thanks to c--'s suggestion to use a different formula.
Binary Lambda Calculus, 125 bits = 15.625 bytes
01000001 11001110 10000101 01100001 10010000 00000101 10011111 01110010
11110111 10110000 00001110 01011110 11010000 10110001 00000100 00010
Encodes the following lambda term:
main = 2 rangesum where
succ = \n f x. f (n f x)
pair = \x y f. f x y
snd = \p. p (\x y. y)
update = \p. p (\x y f. f (succ x) (x succ y))
rangesum = \n. snd (n update (pair 1 0))
This is a function that takes a positive integer in Church numeral and outputs the answer in the same form. The numbers that appear in the Haskell-like code above are also Church numerals, e.g. 2 = \f x. f (f x).
x86-16 machine code, 13 bytes
00000000: 8ad8 43f6 e340 f7e0 b103 d3e8 c3 ..C..@.......
Listing:
8A D8 MOV BL, AL ; BL = x
43 INC BX ; BL = x + 1
F6 E3 MUL BL ; AX = x * (x + 1)
40 INC AX ; AX = x * (x + 1) + 1
F7 E0 MUL AX ; AX = AX * AX
B1 03 MOV CL, 3
D3 E8 SHR AX, CL ; AX = AX >> 3
C3 RET ; return to caller
Callable with input n in AL, output in AX.
Uses the formula (x*(x+1)+1)^2>>3.
Retina 0.8.2, 19 bytes
.+
$*
1
1$`
1
1$`
1
Try it online! Link includes test cases. Explanation:
.+
$*
Convert to unary.
1
1$`
Sum of range.
1
1$`
Sum of range.
1
Convert to decimal.
C (gcc), 22 bytes
With unsequenced modification and access to n
f(n){n*=~n;n=--n*n/8;}
C (clang), 26 bytes
With unsequenced modification and access to n
f(n){n*=~n;return--n*n/8;}
C89, 28 bytes
f(n){n*=~n;return(n-2)*n/8;}
Explanation
f(n){n*=~n;return--n*n/8;} // function which takes and returns an integer
n*=~n; // n *= -(n + 1); here (n = -(n*n + n))
--n // decrement n; here (n = -(n*n + n + 1))
return n*n/8; // return (n*n + n + 1)^2 / 8
Python, 23 bytes
-7 bytes thanks to c--'s suggestion based on tsh's answer.
lambda n:(n*n-~n)**2//8
Python, 38 bytes
lambda n:sum(range(sum(range(n+1))+1))
Nibbles, 2 bytes (4 nibbles)
+,+,
Explanation
+,+,$
$ Input number (implicit)
, Range from 1 to that number
+ Sum
, Range from 1 to that number
+ Sum
Vyxal, 4 bytes
ɾ∑ɾ∑
Uses the straightforward approach: ɾ gets the range from 1 through its argument, and ∑ sums. Rinse and repeat.
SAS 4GL, 22 23 bytes
The code:
o=n*(n+1)*(n*n+n+2)/8;
The code plus "environment" and nice printout:
data;
do n = 1 to 10;
o=n*(n+1)*(n*n+n+2)/8;
put n "-> " o;
end;
run;
The log:
1 data;
2 do n = 1 to 10;
3 o=n*(n+1)*(n*n+n+2)/8;
4 put n "-> " o;
5 end;
6 run;
1 -> 1
2 -> 6
3 -> 21
4 -> 55
5 -> 120
6 -> 231
7 -> 406
8 -> 666
9 -> 1035
10 -> 1540
NOTE: The data set WORK.DATA4 has 1 observations and 2 variables.
NOTE: DATA statement used (Total process time):
real time 0.01 seconds
cpu time 0.01 seconds
Lua, 24 bytes
print((...^2-~...)^2//8)
Based off tsh's answer, though it's been modified a bit to better fit Lua.
Java, 18 17 bytes
n->(n=n*n-~n)*n/8
Port of @tsh' JavaScript answer, so make sure to upvote him/her as well.
-1 byte thanks to @tsh.
(n=n*n-~n) could alternatively be (n+=n*n+1) for the same byte-count:
Try it online.
Explanation:
n-> // Method with integer as both parameter and return-type
(n= // Replace `n` with:
n*n // `n` squared
-~n) // `+n+1`
*n // Square that new `n`
/8 // Integer-divide that by 8 to get the result
A literal implementation would be 56 bytes in comparison:
n->{int t=0;for(;n>0;)t+=n--;for(;t>0;)n+=t--;return n;}
Explanation:
n->{ // Method with integer as both parameter and return-type
int t=0; // Temp-integer, starting at 0
for(;n>0;) // Loop `n` down until it's 0:
t+=n // Add the current `n` to the temp-integer `t`
--; // And then decrease `n` by 1
// (at this point, `n=0`)
for(;t>0;) // Now loop `t` down until it's 0:
n+=t // Add the current `t` to `n`
--; // And then decrease `t` by 1
return n;} // Return the modified `n` as result
MathGolf, 4 bytes
╒Σ╒Σ
Explanation:
╒ # Push a list in the range [1, (implicit) input-integer]
Σ # Sum it together
╒ # Pop and push a list in the range [1, sum]
Σ # Sum it together again
# (after which the entire stack is output implicitly as result)
05AB1E, 4 bytes
LOLO
Try it online or verify all test cases.
Explanation:
L # Push a list in the range [1, (implicit) input-integer]
O # Sum this list together
L # Pop and push a list in the range [1, sum]
O # Sum this list together again
# (after which the result is output implicitly)
Laughing Out Loud! Out??
An equal-bytes alternative could be: 2FLO, where 2F is loop 2 times:
Try it online or verify all test cases.
Charcoal, 10 bytes
I÷⍘12320N⁸
Try it online! Link is to verbose version of code. Explanation:
12320 Literal string `12320`
⍘ Interpret as base
N First input as a number
÷ Integer divide by
⁸ Literal integer `8`
I Cast to string
Implicitly print
The boring solution is 8 bytes:
IΣ…·Σ…·N
Try it online! Link is to verbose version of code. Explanation: InclusiveRange prefers two arguments, but fortunately we can arrange to use the one-argument version (twice, saving two bytes).
N First input as a number
…· Inclusive range from `1` to that
Σ Take the sum
…· Inclusive range from `1` to that
Σ Take the sum
I Cast to string
Implcitly print
><> (Fish), 13 bytes
- -2 bytes thanks to Bubbler
i:1+*2,:1+*2,n;
Just the triangle number formula copied twice
Excel, 26 24 bytes
=A1*(A1+1)/8*(A1^2+A1+2)
Input in cell A1.
Ly, 6 bytes
R&+R&+
A pretty direct mapping of the rules of the contest to Ly instructions for this one...
R - Generate range of numbers from 0 to STDIN
&+ - Sum the stack
R - Generate range of numbers again, from 0 to top-of-stack
&+ - Sum the stack
- Top of stack printed as a number be default on exit
APL (Dyalog Unicode), 6 bytes
Anonymous tacit prefix function.
+/∘⍳⍣2
+/ the sum
∘ of
⍳ the range
⍣2 twice
C, 65 bytes
f(n){int i=10;for(;i>1;)n+=i--;for(;n>1;)i+=n--;printf("%d",i);}
Earlier attempts
73 bytes
main(){int i=0,n=10;for(;n>0;)i+=n--;for(;i>0;)n+=i--;printf("%d\n",n);}
70 bytes
main(n){int i=10;for(;i>1;)n+=i--;for(;n>1;)i+=n--;printf("%d\n",i);}

