# Sum of a range of a sum of a range of a sum of a range of a sum of a range of a sum of

### Inspired by the fact that a few related challenges to this could be answered by Vyxal in 0 Bytes using a special flag combination.

Given only one input integer $$\n\$$, calculate $$\f(n,n)\$$ where $$f(x,y)= \begin{cases} x & \text{if } y=0 \\ f(\left(\sum_{k=1}^xk\right),\text{ }y-1) & \text{otherwise} \end{cases}$$ If you want an explanation in plain English, here it is, quoted from OEIS:

Let $$\T(n)\$$ be the $$\n\$$-th triangular number $$\n*(n+1)/2\$$; then $$\a(n)\$$ = $$\n\$$-th iteration [of] $$\T(T(T(...(n))))\$$.

Note that a(n) is the function.

This is also A099129$$\(n)\$$, but with the case for $$\n=0\$$. This is , so as long as you make your answer short, it doesn't matter whether it times out on TIO (my computer can't calculate $$\n=6\$$ within five minutes!). Yes, standard loopholes apply.

## Test cases:

0 -> 0
1 -> 1
2 -> 6
3 -> 231
4 -> 1186570
5 -> 347357071281165
6 -> 2076895351339769460477611370186681
7 -> 143892868802856286225154411591351342616163027795335641150249224655238508171

• I have a 2 byte vyxal implementation but it feels unfair to post it... Jul 11, 2023 at 17:49
• Note that adding flags changes the language on this site, "vyxal" != "vyxal -xyz" etc..., so do you really have a 2 byte "vyxal" implementation? :p Jul 13, 2023 at 18:24
• @JonathanAllan Oh yeah it’s Vyxal R Jul 13, 2023 at 19:00

# 05AB1E, 3 bytes

FLO


Try it online!

#### Explanation

     # Implicit input
F    # Repeat input number of times:
L   #  Push the one-range of top of stack
O  #  Sum the resulting list
# Implicit output

• Gotta go with the flo(w)!
– Neil
Jul 11, 2023 at 20:27

# Python 2, 35 bytes

def f(n):exec"n=n*-~n/2;"*n;print n


Try it online!

A function that prints.

# Python 3, 39 bytes

lambda n:eval("(n:=n*-~n//2)*0+"*n+"n")


Try it online!

The annoying thing in Python 3 here is that exec within a function is its own scope and doesn't affect variables outside the exec, so we can't port the code like def f(n):exec("n=n*-~n/2;"*n);print(n) -- the n will be unchanged.

The code above works around this by evaluating a string like (n:=n*-~n//2)*0+(n:=n*-~n//2)*0+n that modifies n using the walrus := and evaluates to n within its own scope. If we didn't need n=0, we could do:

37 bytes, fails n=0

lambda n:eval("(n:=n*-~n//2),"*n)[-1]


Try it online!

Here were some other attempts.

39 bytes

lambda n:eval('('*n+'n'+'+.5)**2//2'*n)


Try it online!

Based on @dingledooper's comment.

40 bytes

def f(n):exec("n*=n/2+.5;"*n+"print(n)")


Try it online!

41 bytes

lambda n:0*[n:=n*-~n//2for _ in[1]*n]or n


Try it online!

• Also a cute 36 for py2 is lambda L:eval('('*L+'L*2+1)**2/8'*L) (it does fail for n=0 though). It takes advantage of the 'L' suffix to save a couple bytes. Sadly I'm not aware of any "feature" like this for py3. Jul 12, 2023 at 7:31

# Python 3, 42 bytes

think i might be able to get it down further... lmk if you guys see anything !!

lambda x:eval("sum(range("*x+"x"+"+1))"*x)


Try it online!

• Using xnor's calculation I can get 40 bytes, but py3.8+ only. Jul 11, 2023 at 22:40

# Jelly, 4 bytes

RS$¡  Try it online! Full program, takes $$\n\$$ as the first argument and outputs $$\f(n, n)\$$ ## How it works RS$¡ - Main link. Takes n on the left
$- Last two links as a monad f(n): R - Range [1, ..., n] S - Sum; T(n) ¡ - Apply f n times, beginning with n  # ><> (Fish), 23 bytes i::?\~n; -10.\r:1+*2,r1  Try it # Brainfuck, 64 48 bytes [>+>+<<-]>[->[[>+>+<<-]>>-[<<+>>-]<<]>[<+>-]<<]>  Try it online! (Up to n = 3 with 8 bit numbers, n = 4 with 32 bit, n = 5 with 64 bit, n = 6 possibly never [with BigInt].) Rather simple brainfuck answer. Assumes you initialise the tape with the input on cell 0 and stores the output in cell 2. If input given in binary, just wrap the code in , and . for +2 bytes. If the input should be the number in ASCII this would also involve writing the parser, I feel that is not in the spirit of the challenge. The input in the first cell (0) is duplicated and then the loop simply iterates on cell 1. Each value of the triangle is added to cell 3 and copied back with 1 removed to cell 2. At the end of the loop that new value is stored in cell 2. Header: give N here ++++ Actual code [>+>+<<-] # Duplicate >[- # Repeat input times: >[ # While x = n can be decremented [>+>+<<-] # Accumulate result and make copy >>-[<<+>>-]<< # Move copy minus 1 back ]>[<+>-]<< # Move back accumulated T(n) as new x ]>  # Pyth, 6 bytes LsSbyF  Try it online! ### Alternative 6 byte solution usSGQQ  Try it online! ### Another alternative 6 byte solution .v*"sS  Try it online! This one's my favorite. ### Another alternative 6 byte solution em=sSQ  Try it online! Feels like there's gotta be a 5 byte solution but I got nothing # Factor + math.unicode, 26 bytes [ dup [ [1,b] Σ ] times ]  Try it online! Literally just take the sum of the range of the input <input> number of times. Since sum dispatches on ranges to use the arithmetic formula, it's even efficient. # Thunno 2, 3 bytes {RS  Attempt This Online! #### Explanation {RS # Implicit input { # Repeat input number of times: R # One-range of top of stack (initially the input number) S # Sum the resulting list and leave it for the next iteration # Implicit output  # R≥4.1, 33 bytes \(x){Map(\(i)x<<-sum(0:x),1:x);x}  Attempt This Online! R afficionados should wince when they see Map used for it's out-of-scope side-effect due to <<- assignment, and the result discarded... # R<4.1, 42 bytes f=function(x,y=x)if(y,f(sum(1:x),y-1),x)  Attempt This Online! Without the short \ form, it's too expensive to define two functions. • For the second solution, assigning the two-argument recursive function to ? instead of f is one byte shorter, as usual :) Jul 12, 2023 at 5:54 • 32 bytes, no <<- needed! Jul 19, 2023 at 1:51 # Ruby, 34 bytes f=->x,y=x{y<1?x:f[(1..x).sum,y-1]}  Try it online! # Python 3, 63 62 bytes f=lambda x,y=-1:f(x,x)if y<0else x if y<1else f(x*(x+1)/2,y-1)  Try it online! Calculates a(n) using the OEIS definition given in the OP, probably still golfable Edit: fixed inputs and added "f=" # BQN, 9 6 bytes Edit: -3 bytes thanks to ovs +´⟜↕⍟⊣  Try it at BQN REPL +´⟜↕⍟⊣ ⍟ # Repeat ⊣ # number of times equal to argument +´⟜↕ # this function: ↕ # range 0..(arg-1) ⟜ # as left arg to +´ # fold-addition # with starting value of function arg # (so: sum of 0..arg)  • This can be (⊢+´↕)⍟⊣ (left argument of reduction is seed value), which can be further shortened to +´⟜↕⍟⊣. – ovs Jul 11, 2023 at 21:35 # J, 11 10 bytes (-:*>:)^:]  Requires extended precision integer as input. -1 thanks to @Bubbler. Attempt This Online! (-:*>:)^:]­⁡​‎‎⁪⁡⁪⁠⁪⁢⁤⁪‏⁠‎⁪⁡⁪⁠⁪⁣⁡⁪‏‏​⁡⁠⁡‌⁢​‎‎⁪⁡⁪⁠⁪⁣⁢⁪‏‏​⁡⁠⁡‌⁣​‎‎⁪⁡⁪⁠⁪⁢⁡⁪‏⁠‎⁪⁡⁪⁠⁪⁢⁢⁪‏‏​⁡⁠⁡‌⁤​‎‎⁪⁡⁪⁠⁪⁢⁪‏⁠‎⁪⁡⁪⁠⁪⁣⁪‏⁠⁪⁪‏​⁡⁠⁡‌⁢⁡​‎‎⁪⁡⁪⁠⁪⁤⁪‏‏​⁡⁠⁡‌­ ^: NB. ‎⁡repeat ] NB. ‎⁢y times >: NB. ‎⁣increment -: NB. ‎⁤halve * NB. ‎⁢⁡multiply  # J, 12 11 bytes (-:*>:)^:x:  Attempt This Online! (-:*>:)^:]­⁡​‎‎⁪⁡⁪⁠⁪⁢⁤⁪‏⁠‎⁪⁡⁪⁠⁪⁣⁡⁪‏‏​⁡⁠⁡‌⁢​‎‎⁪⁡⁪⁠⁪⁣⁢⁪‏‏​⁡⁠⁡‌⁣​‎‎⁪⁡⁪⁠⁪⁢⁡⁪‏⁠‎⁪⁡⁪⁠⁪⁢⁢⁪‏‏​⁡⁠⁡‌⁤​‎‎⁪⁡⁪⁠⁪⁢⁪‏⁠‎⁪⁡⁪⁠⁪⁣⁪‏⁠⁪⁪‏​⁡⁠⁡‌⁢⁡​‎‎⁪⁡⁪⁠⁪⁤⁪‏‏​⁡⁠⁡‌­ ^: NB. ‎⁡repeat x: NB. ‎⁢y times but promote to extended precision >: NB. ‎⁣increment -: NB. ‎⁤halve * NB. ‎⁢⁡multiply  • (-:*>:)^:] works with extended precision integers. Jul 12, 2023 at 10:14 • Ah, good catch! Jul 12, 2023 at 18:01 # JavaScript (Node.js), 29 bytes f=(x,y=x)=>y?f(x*++x/2,y-1):x  Try it online! # Husk, 5 bytes !→¹¡Σ  Try it online! # Racket – 52 bytes (define(f x y)(if(= y 0)x(f(/(*(+ x 1)x)2)(- y 1))))  Try it online! ## Explanation We define a function f that receives two arguments: x and y where y is used as decrementing iterator that when equal to zero will make f return the result of x. (define (f x y) (if (= y 0) x (f (/ (* (+ x 1) x) 2) (- y 1))))  We can represent this function mathematically using the following: $$f(x, y) = \begin{cases} x & y = 0 \\ f(\frac{x(x+1)}{1}, y - 1) & y > 0 \end{cases}$$ ## Sub-optimal variations To allow user input I could've done: (let([n(read)])(let f([x n][y n])(if(= y 0)x(f(/(*(+ x 1)x)2)(- y 1)))))  Which consists of 72 bytes when minified. We could also have used a combination of a lambda and let statement to save one byte: ((λ(n)(let f([x n][y n])(if(= y 0)x(f(/(*(+ x 1)x)2)(- y 1)))))(read))  (The λ character equals two bytes.) ## Conclusion Have an amazing day! I am still mind blown at Racket's speed and capability of handling large numbers. At $$\n = 17\$$, TIO can't seem to render all the digits alongside all iterations of the for loop in the footer. # Charcoal, 13 bytes ＮθＦθ≔Σ…·¹θθＩθ  Try it online! Link is to verbose version of code. Explanation: Ｎθ  Input n. Ｆθ  Repeat n times... ≔Σ…·¹θθ  ... replace the current value the sum of its range. Ｉθ  Output the final value. 14 bytes for an efficient version: ＮθＦθ≔÷×⊕θθ²θＩθ  Try it online! Link is to verbose version of code. Explanation: Uses the triangle formula to sum the range. # VyxalR, 14 bitsv2, 1.75 bytes (∑  Try it Online! Yeah... ( # Input number of times: ∑ # Sum top of stack # (R flag makes it take the range instead of the digits)  • uhm ackshully, this can be shorter if you use vyncode, a fractional byte compression scheme first introduced in vyxal 2.21.0🤓 Jul 12, 2023 at 5:34 • At least it’s >0 Jul 12, 2023 at 7:36 • exactly what I was thinking of Jul 12, 2023 at 10:48 • Why is this not titled "vyncode -R", or perhaps "Vyxal -R="? (I'm not familiar with the implementation). Jul 13, 2023 at 19:08 • @JonathanAllan I had mentioned it to lyxal earlier but it seems everyone just uses the template that I've used in this answer. The online interpreter generates that template when you click on the CGCC post button. Jul 13, 2023 at 19:12 # J-uby, 23 bytes ~(:-&(:**&(:+|:sum))).D  Attempt This Online! ## Explanation With some whitespace: ~(:- & (:** & (:+ | :sum))).D  The core function here is :** & (:+ | :sum), where :+ constructs a range and :sum sums it, and :** applies that function to its own result n times. That does what we want—but only if we call it like F.(n).(n). In order to be able to call it like F.(n), we use :- to turn it into F.(n, n), D to force F to be treated as a dyad, and then ~ to repeat the given argument the necessary number of times (which is two, thanks to D). # MATL, 5 bytes t:":s  Try it online! ### How it works t % Input (implicit). Duplicate : % Range " % For each : % Range s % Sum % End (implicit). Display (implicit)  # Nekomata, 4 bytes ᵑ{R∑  Attempt This Online! ᵑ{R∑ ᵑ{ Repeat R Range ∑ Sum  # Wolfram Language (Mathematica), 20 bytes Nest[#(#+1)/2&,#,#]&  Try it online! This code simply nests the short-hand expression for the triangular numbers the given number of times. The computation time starts to become noticeable around n=20 and my laptop takes around 50 seconds to compute n=27, which is a number that takes 63.5 MB to store. # Brachylog, 8 bytes ;?{⟦+}ⁱ⁾  Try it online! ### Explanation ;?{ }ⁱ⁾ Iterate <Input> times on the Input: ⟦ Range + Sum  # Haskell, 29 bytes (!!)=<<iterate(\x->sum[0..x])  Expands to \n -> iterate (\x -> sum [0..x]) n !! n  Try it online! # x86-64 machine code, 17 bytes Standard System V ABI, signature long fsor(int n) (n is taken in edi, and the output is placed in rax). 57 58 50 5e ff c6 48 f7 e6 48 d1 e8 ff cf 7f f2 c3  Try it online! ## Explanation 0000000000000000 <fsor>: 0: 57 push rdi ; cnt (edi) 1: 58 pop rax ; n = cnt ; 0000000000000002 <__fsor_lp>: ; start_loop: 2: 50 push rax ; 3: 5e pop rsi ; n1 = n 4: ff c6 inc esi ; n1++ 6: 48 f7 e6 mul rsi ; n *= n1 9: 48 d1 e8 shr rax,1 ; n >>= 1 c: ff cf dec edi ; cnt-- e: 7f f2 jg 2 <__fsor_lp> ; if (cnt > 0) goto start_loop 10: c3 ret ; return  # Dyalog APL, 10 bytes {+/⍤⍳⍣⍵⊢⍵}  ## Explanation  ⍤ ⍝ ‎⁡Function composition: +/ ⍝ ‎⁢ sum reduce ⍳ ⍝ ‎⁣ the numbers from 1 to the argument of the composed function ⍣⍵ ⍝ ‎⁤repeated input times ⊢⍵ ⍝ ‎⁢⁡starting from the input 💎 Created with the help of Luminespire at https://vyxal.github.io/Luminespire  # Raku, 25 bytes {($_,{.++*$_/2}...*)[$_]}


Try it online!

# Julia 1.0, 33 26 bytes

~x=(1:x.|>_->x*=(x+1)/2;x)


Try it online!

-7 bytes thanks to MarcMush: replace for loop with pipe operator |>

## Alternate approach, 39 35 bytes

~x=x|>∘(fill(n->n*(n+1)÷2,x)...)


Attempt This Online!

-4 bytes thanks to MarcMush: remove call to reduce

# Alice, 24 20 bytes

/O
\I@/.!wrd&+?t.!$K  Try it online! Times out for larger values as it creates a range from 0 to the sum of the last range. ## Alice, 21 bytes /O \I@/.!w.h*2:?t.!$K


Try it online!

Does not time out as it uses the n*(n+1)/2 formula instead