# 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... Commented 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 Commented Jul 13, 2023 at 18:24
• @JonathanAllan Oh yeah it’s Vyxal R Commented Jul 13, 2023 at 19:00

# Retina, 18 bytes

.+
*
"$+"+_$>
_


Try it online! No test suite due to the way the program uses history. Explanation:

.+
*


Convert n to unary.

"$+"+  Repeat n times. _$>


Replace the current value with its range, effectively taking the sum.

_


Convert to decimal.

# MathGolf, 6 4 bytes

_Å╒Σ


Try it online.

Explanation:

_      # Duplicate the (implicit) input-integer†
Å     # Pop one, and loop that many of times,
# using the following two characters as inner code-block:
╒    #  Pop an integer, and push a list in the range [1,integer]
Σ   #  Pop a list, and push its sum
# (after the loop, the entire stack is output implicitly as result)


† The initial duplicate _ is necessary despite having implicit inputs for two reasons:

1. MathGolf uses 0 if a stack is empty and it needs an integer. Although I think it's a bug in MathGolf (I'm not 100% sure), unfortunately that 0 will take priority over the implicit input-integer in the first iteration of a for-loop. So for any $$\n>0\$$, just Å╒Σ will result in 0 because Å will push a 0 in its first iteration and use it for the ╒, instead of using the (implicit) input-integer.
2. For $$\n=0\$$, the Å╒Σ will only pop the (implicit) input-integer and leave the stack empty, and thus it'll output an empty string as result. The initial _ will ensure a second 0 is still on the stack to be output instead.
• Wow mathgolf doing everything except what the challenge requires without the _ Commented Jul 14, 2023 at 12:14

# Python + vyxal, 68 Bytes

lambda x:execute_vyxal("","eR"+"s"*x,str(x))
from vyxal.main import*


With the power of Python, I can make dynamic flags!*

The vyxal package can be installed using pip install vyxal.

# sed, 73 bytes

s/!*/&''&/g
:a
s/<(!*)''!(!*>)/<'\1'\2/
s/(<!*)'(!*)!/\1\2!'\2/
ta
s/'//g


Try it online!

Input is unary, with exclamation marks, surrounded by angle brackets. Output is unary with exclamation marks, surrounded by angle brackets. In the TIO link I made it convert to decimal for the output, so that it's easier to see. If you would like to see the unary output, just delete everything in the footer section.

$\=$_;$\*=$\++/2while$_--}{  Try it online! # C (gcc), 36 bytes x;a(n){for(x=n;n--;x/=2)x+=x*x;n=x;}  Try it online! Was missing a semicolon, so now it's 36 bytes. • -1 byte – c-- Commented Jul 18, 2023 at 22:02 • @c-- Thanks for the tip. And that's a beautiful statement there: x*=-~x/2.;. Took me nearly a half hour to fully grok it. Commented Jul 19, 2023 at 2:50 • maybe something like x*=-.5*~x; makes more sense, but I'm used to looking at -~x as (x+1) thanks to this tip – c-- Commented Jul 19, 2023 at 4:21 • I'm familiar enough with bit twiddling to catch that part, it was the floating-point and type coercion that caught me. Then I tried it without the decimal point and figured it all out. Thanks for the teaching tips. Commented Jul 19, 2023 at 4:31 • Right, implicit conversions are very useful. Anytime :) – c-- Commented Jul 19, 2023 at 4:35 # Desmos, 45 bytes Input is in a variable n. Output is placed in variable f. Start the ticker to run the program. To reset, run the action in the "reset" folder. Ticker: \left\{k<n:f->f(f+1)/2\right\},k->k+1  Expression List: f=n k=0  Try it on Desmos! Explaination: Applies $$\f\to T(f)\$$ n times, using a ticker because Desmos does not support recursion. # ForWhile, 15 bytes {:(.0'(+:):)}1$

same length, but faster (uses formula for sum of first n numbers):

{:(.::*+2/:)}1$ online Interpreter # Explanation { }1$ defines a function, store it at id 1

the argument is read from the stack

: duplicate the argument

(. loop but ignore the loop counter

0'(+:) compute the triangle number corresponding to the top value on the stack

:) end of for loop, now the next triangle number is on the stack

returns the result on the stack

Example for how to call the function

{:(.0'(+:):)}1\$ \ the actual function

4 1@? \ call the function with argument 4

\ print the top stack value as an integer, displaying stack values directly is not supported by this version of the interpreter
0;1+(.;10%"0".+;2+~,1+'10/';)'.(,#)10#.
\ alternatively you can read the stack contents in the browser console from the 'valueStack' variable


The interpreter is newer than the challenge, but it implements he same language as the original C interpreter.

• Amazing that "The interpreter is newer than the challenge" when the challenge is still only 2h old, but somehow I'm afraid the link doesn't work for me... Commented Jul 11, 2023 at 20:23
• I have worked on porting the language to JavaScript for a while now, but only made the (unfinished) version public after the challange was posted. The link might not work since I created the GitHub page just a few minutes ago Commented Jul 11, 2023 at 20:26

# Japt-h, 6 bytes

õ@=ô x


Try it

## 5 bytes

If we could output undefined for 0
(or I could use the -hF0 flags to output 0 but I hate Japt's -E & -F flags!)

Æ=õ x


Try it

• It has to output 0 for 0, so the 6 byte solution is the valid one. Commented Jul 12, 2023 at 10:49
• What’s wrong with -E and -F ? Commented Jul 12, 2023 at 10:58

# Go, 56 bytes

func(n int)int{k,i:=n,0
for;i<n;i++{k=k*-^k/2}
return k}


Attempt This Online!

Maxes out at n=5.

# Go, 138 bytes, math/big

import."math/big"
func f(n int)*Int{N:=NewInt
k,i,z:=N(int64(n)),0,N(0)

⍥(/++1⇡).