# 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

# JavaScript (Node.js), 17 bytes

x=>(x*x-~x)**2>>3


Try it online!

Thanks Kevin Cruijssen for -1 byte.

• +x+1 can be -~x for -1 byte. Commented May 16, 2023 at 9:42

# C (gcc), 22 bytes

With unsequenced modification and access to n

f(n){n*=~n;n=--n*n/8;}


Try it online!

# C (clang), 26 bytes

With unsequenced modification and access to n

f(n){n*=~n;return--n*n/8;}


Try it online!

# C89, 28 bytes

f(n){n*=~n;return(n-2)*n/8;}


Try it online!

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


# Wolfram Language (Mathematica), 15 bytes

#@*#&@Tr@*Range


Try it online!

-1 byte from @att

# Wolfram Language (Mathematica), 16 bytes

(f=Tr@*Range)@*f


Try it online!

• #@*#&@Tr@*Range
– att
Commented May 19, 2023 at 20:11

# APL (Dyalog Unicode), 6 bytes

Anonymous tacit prefix function.

+/∘⍳⍣2


Try it online!

+/ the sum

∘ of

⍳ the range

⍣2 twice

# Japt-x, 5 bytes

õ x õ


Try it

• Just that the number of bytes encoding-dependent, utf-8 would be 7 bytes ;) Commented May 17, 2023 at 7:18
• @Aconcagua Actually, Japt is designed to be used with this encoding specifically, as the interpreter can read those raw bytes as they are in that codepage Commented May 17, 2023 at 11:44

# Brain-Flak, 32 bytes

({({}[()])()}{})
({({}[()])()}{})


Try it online!

(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)
{}
)


g.g
g n=sum[1..n]


Try it online!

Just implementing the definition seems to be shorter than arithmetical expressions such as

21 bytes

f n=((n^2+n+1)^2-1)/8


Try it online!

With floats:

17 bytes

g.g
g n=n*(n+1)/2


Try it online!

# Nibbles, 2 bytes (4 nibbles)

+,+,


Attempt This Online!

### Explanation

+,+,  Input number (implicit)
,   Range from 1 to that number
+    Sum
,     Range from 1 to that number
+      Sum


# Binary Lambda Calculus, 125 bits = 15.625 bytes

01000001 11001110 10000101 01100001 10010000 00000101 10011111 01110010
11110111 10110000 00001110 01011110 11010000 10110001 00000100 00010


Verify it online!

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

# Charcoal, 10 bytes

Ｉ÷⍘12320Ｎ⁸


Try it online! Link is to verbose version of code. Explanation:

   12320    Literal string 12320
⍘         Interpret as base
Ｎ   First input as a number
÷          Integer divide by
⁸  Literal integer 8
Ｉ           Cast to string
Implicitly print


The boring solution is 8 bytes:

ＩΣ…·Σ…·Ｎ


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

       Ｎ    First input as a number
…·     Inclusive range from 1 to that
Σ       Take the sum
…·        Inclusive range from 1 to that
Σ          Take the sum
Ｉ           Cast to string
Implcitly print


# 05AB1E, 4 bytes

LOLO


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.

• I love that these make readable words Commented May 16, 2023 at 22:02

# Vyxal, 4 bytes

ɾ∑ɾ∑


Try it Online!

Uses the straightforward approach: ɾ gets the range from 1 through its argument, and ∑ sums. Rinse and repeat.

# Pyth, 4 bytes

sSsS


Try it online!

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

# R, 19 bytes

\(n)sum(1:sum(1:n))


Attempt This Online!

• Attempt this online has a version of R that's on 4.3.0, so you can use it to include a link if you like. Commented May 19, 2023 at 17:25

# brainf**k, 110 bytes

,[>+>+>+<<<-]>>>[-[>+>+<<-]>>[-<<+>>]<<[-<+>]>[-<+>]<]<[>+>+>+<<<-]>>>[-[>+>+<<-]>>[-<<+>>]<<[-<+>]>[-<+>]<]<.


Try it online!

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.

• I'd suggest changing the test case to this (where ascii 5 turns into x which is 120 in ascii), since when 4 is the input one expects 55 as the output, but your code returns 243 Commented Jul 18, 2023 at 14:45
• @CommandMaster Problem is, the code seems to tend to interpret numeric inputs as ascii values instead of their values. Try replacing "," with "++++" if you wanna test 4. You can view the running process on the online IDE I linked to. Commented Jul 19, 2023 at 1:48
• By the way, I suppose this could be golfed to around 80 bytes or so by introducing an outer loop, but my brain is already scorched so I'll leave that up to the more f**kable brains ;-P Commented Jul 19, 2023 at 1:51

# Desmos, 21 20 bytes

N=nn+n
f(n)=N(N+2)/8


Try It On Desmos!

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

Try it online.

(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;}


Try it online.

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

• Maybe you can write /8 instead of >>3 in Java.
– tsh
Commented May 16, 2023 at 10:06
• @tsh I indeed can. Thanks. Commented May 16, 2023 at 10:34

# PARI/GP, 16 bytes

-2 byte thanks to @tsh.

n->(1+n^2+n)^2\8


Attempt This Online!

• I know nothing about PARI/GP. But seems n->(1+n^2+n)^2>>3 works.
– tsh
Commented May 16, 2023 at 9:41

# Python, 23 bytes

-7 bytes thanks to c--'s suggestion based on tsh's answer.

lambda n:(n*n-~n)**2//8


Attempt This Online!

# Python, 38 bytes

lambda n:sum(range(sum(range(n+1))+1))


Attempt This Online!

• 23 bytes porting tsh's JS answer
– c--
Commented May 16, 2023 at 15:11

t n=sum[1..sum[1..n]]


Try it online!

# 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


Callable with input n in AL, output in AX.

Uses the formula (x*(x+1)+1)^2>>3.

• 186 and later can use shr ax, 3 (3 bytes). (If you meant to stick to a tighter restriction of only 8088/8086 instructions, I assume you would have said that instead of x86-16, which includes modern x86 in 16-bit mode.) Commented May 19, 2023 at 8:42

# Julia, 16 bytes

!n=(n^2-~n)^2÷8


Attempt This Online!

The formula looks a bit shorter than directly writing out the sums.

# Go, 41 37 bytes

func(n int)int{n=n*n-^n
return n*n/8}


Attempt This Online!

• -4 bytes by @c--
• @c-- In Go, the tilde is not used as bitwise negation like in other languages; instead it's used for type constraints (~A is "types that have underlying type A"). Try running your own port and look at the compiler error. Commented May 17, 2023 at 15:23

# Arturo, 18 bytes

\$=>[∑1..∑1..&]


Try it!

# Factor + math.unicode, 21 bytes

[ [1,b] Σ [1,b] Σ ]


Try it online!

# Husk, 2 bytes

‼Σ


Try it online!

‼         # Apply function twice:
Σ         # Triangular number


(the more-boring ΣΣ is also 2 bytes...)

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

# ><> (Fish), 13 bytes

• -2 bytes thanks to Bubbler
i:1+*2,:1+*2,n;


Try it

Just the triangle number formula copied twice

• 13 bytes: i:1+*:2+*8,n; using the formula expanded as n(n+1)(n(n+1)+2)/8. Commented May 16, 2023 at 7:34

# Thunno 2, 4 bytes

RSRS


Attempt This Online!

Range, sum, range, sum.

• Nice one! You can also add the S flag and drop the last S. Commented May 16, 2023 at 16:56
• @TheThonnu Good point, I don’t think I’ll go through and edit my answer though, as this is such a trivial challenge. Commented May 16, 2023 at 17:03

# Lua, 24 bytes

print((...^2-~...)^2//8)


Try it online!

Based off tsh's answer, though it's been modified a bit to better fit Lua.