# sum of a range of a sum of a range of n

In this challenge, your input is an integer value. Your task is to find the sum of the range of the sum of the range of n.

Examples:

Input -> Output
1     -> 1
2     -> 6
3     -> 21
4     -> 55
5     -> 120
6     -> 231
7     -> 406
8     -> 666
9     -> 1035
10    -> 1540


This challenge should be fairly simple to complete in most languages :)
Have fun!

• OEIS: A002817 Commented May 16, 2023 at 9:04
• Is the input an integer, or as in your example a strictly positive integer ? Commented May 17, 2023 at 12:52
• T_(T_n), where T_n is the nth triangle number. Commented Jan 4 at 17:33

# 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.

# 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.

• idk rockstar but this seems to work for 56 bytes: listen to N cast N let N be N*N+N let M be N+2 say N*M/8
– c--
Commented May 16, 2023 at 16:57

# Jelly, 4 bytes

RSRS


Attempt This Online!

Nobody golfing in Jelly anymore?

• Polyglots with Thunno 2! Commented May 17, 2023 at 18:21

# Pyt, 2 bytes

△△


Try it online!

Uses the built-in for triangle number twice. Implicit input and output.

# 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

• +´⟜↕⍟2 (+´⟜↕x=x+´↕x)
– att
Commented May 19, 2023 at 20:13
• @att Oh, that's clever. I often forget that folds and scans can be dyadic. Commented May 19, 2023 at 22:33

# K (ngn/k), 9 bytes

2{x+/!x}/


Try it online!

• 2{...}/ set up a monadic do-reduce, running the code in {...} twice
• !x generate 0..x-1
• x+/ set up a plus-reduce (i.e. sum) seeded with the input x
• Tacit: 2(+/!1+)/. No shorter though.
– doug
Commented Aug 20, 2023 at 4:21

# Ly, 6 bytes

R&+R&+


Try it online!

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


# Excel, 26 24 bytes

=A1*(A1+1)/8*(A1^2+A1+2)


Input in cell A1.

# MathGolf, 4 bytes

╒Σ╒Σ


Try it online.

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)


# PHP, 25 bytes

fn($n)=>($n**2-~$n)**2>>3  Attempt This Online! # 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  # Nim, 36 bytes func a[I](n:I):I=(n*n+n)*(n*n+n+2)/8  Attempt This Online! Takes and returns a float (so I can use / instead of div). # 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! # minigolf, 7 bytes 2,,n;+_  Try it online! ## Explanation 2, _ Repeat 2 times: ,n; Generate a [1..n] range + Sum it Implicit output  # ARBLE, 13 bytes (n^2-~n)^2//8  Direct port of bluswimmer's port of tsh's answer, utilising ARBLE's more terse extensions. Try it online! # AWK, 23 bytes $0=int((1+$0^2+$0)^2/8)


Try it online!

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  • 24 bytes to print if input is zero, I think... Commented Jun 8, 2023 at 7:17 • @DominicvanEssen you are right ! Didn't bother too much with the second one as I didn't get an answer from OP Commented Jun 8, 2023 at 7:35 # Bash/Zsh, 19 bytes $[($1**2-~$1)**2/8]


Try it online!

# K (ngn/k), 15 bytes

-8!*/0 2+*/0 1+


Try it online!

# 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}


Try it online!

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.

# AWK, 21 bytes

$0*=$0+1,$0*=($0+2)/8


Attempt This Online!

Given a positive integer as input, the following happens in sequence:

• The pattern $0*=$0+1 is tested. $0 initially contains the input number n, and is overwritten to n^2+n. Since this is nonzero, the pattern is matched, and a "range match" starts. • The pattern $0*=($0+2)/8 is tested. $0 is 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. # Trilangle, 15 bytes ?22)d**2'd')2p@  Try it on the online interpreter! Just the code for "sum range" twice. # Neim, 4 bytes 𝐈𝐬𝐈𝐬  Explanation: 𝐈 # inclusive range 𝐬 # sum range 𝐈 # inclusive range 𝐬 # sum range  Try it online! # 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(" ")) } }  • you don't need the space after the first to – Jo King Commented May 25, 2023 at 6:02 # J-uby, 12 bytes (:+|:sum)**2  Attempt This Online! # Moonscript/Yuescript, 18 bytes (n)->(n^2-~n)^2//8  Try it online! # 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. • I guess simple "x^2+x; halve by shift; x^2+x; halve by shift" has the same byte count? Commented Jul 13, 2023 at 6:23 • yeah you're right, and it takes a higher input before overflowing too, 92681 is the max instead of 65535. i actually had a comment that i deleted saying "wait maybe i copied the wrong formula". i think i'll change it. Commented Jul 13, 2023 at 15:23 # Racket + math, 38 bytes (sum(range(+(sum(range(+(read)1)))1)))  Try it online! 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)))  Try it online! • 46 bytes, no math import (apply +(range(+(apply +(range(+(read)1)))1)))  Try it online! • 36 bytes, using triangle-number from math, it's fast and -2 bytes, but might be somewhat cheating? ((λ(t)(t(t(read))))triangle-number)  Try it online! # UiuaSBCS, 9 bytes ⍥(/++1⇡)2  Try it! # Perl 5-MList::Util=sum -p, 19 bytes $_=sum 1..sum 1..\$_


Try it online!

# VyxalRss, 0 bytes




Try it Online!

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.

• I'd say the code is the flags in this case so 3 bytes Commented May 16, 2023 at 7:40
• By that logic python -c "print('hello world')" is also 0 bytes. If the program is entirely flags then the flags is the program Commented May 16, 2023 at 7:43
• Most others didn't chain multiple operators. I think those are cheaty too but in those cases the flags are not literally just multiple instructions run in sequence like a program would run Commented May 16, 2023 at 7:48
• @mousetail, the consensus is here if you want to argue against it, rather than in the comments of individual solutions adhering to that consensus. Commented May 16, 2023 at 8:42
• @mousetail this is a flags only program - R is a behavioural flag which changes how numbers are converted to iterables (usually it's list of digits, but with R it's range) and the two s are output flags. It's interesting because uses a little known quirk of the flag handler, which is that duplicate flags aren't stripped and applied in the for loop that applies output flags. Commented May 16, 2023 at 9:08