26
\$\begingroup\$

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
\$\endgroup\$
3
  • 1
    \$\begingroup\$ I have a 2 byte vyxal implementation but it feels unfair to post it... \$\endgroup\$ Commented Jul 11, 2023 at 17:49
  • 2
    \$\begingroup\$ 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 \$\endgroup\$ Commented Jul 13, 2023 at 18:24
  • \$\begingroup\$ @JonathanAllan Oh yeah it’s Vyxal R \$\endgroup\$ Commented Jul 13, 2023 at 19:00

41 Answers 41

1
2
1
\$\begingroup\$

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.

\$\endgroup\$
1
\$\begingroup\$

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.
\$\endgroup\$
1
  • \$\begingroup\$ Wow mathgolf doing everything except what the challenge requires without the _ \$\endgroup\$ Commented Jul 14, 2023 at 12:14
1
\$\begingroup\$

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.


*Oh wait dynamic flags sound very useful
\$\endgroup\$
1
\$\begingroup\$

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.

\$\endgroup\$
1
\$\begingroup\$

Perl 5, 27 bytes

$\=$_;$\*=$\++/2while$_--}{

Try it online!

\$\endgroup\$
2
1
\$\begingroup\$

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.

\$\endgroup\$
5
  • \$\begingroup\$ -1 byte \$\endgroup\$
    – c--
    Commented Jul 18, 2023 at 22:02
  • 1
    \$\begingroup\$ @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. \$\endgroup\$
    – Saladin
    Commented Jul 19, 2023 at 2:50
  • \$\begingroup\$ maybe something like x*=-.5*~x; makes more sense, but I'm used to looking at -~x as (x+1) thanks to this tip \$\endgroup\$
    – c--
    Commented Jul 19, 2023 at 4:21
  • \$\begingroup\$ 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. \$\endgroup\$
    – Saladin
    Commented Jul 19, 2023 at 4:31
  • \$\begingroup\$ Right, implicit conversions are very useful. Anytime :) \$\endgroup\$
    – c--
    Commented Jul 19, 2023 at 4:35
1
\$\begingroup\$

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.

\$\endgroup\$
1
\$\begingroup\$

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.

\$\endgroup\$
2
  • 1
    \$\begingroup\$ 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... \$\endgroup\$ Commented Jul 11, 2023 at 20:23
  • \$\begingroup\$ 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 \$\endgroup\$
    – bsoelch
    Commented Jul 11, 2023 at 20:26
0
\$\begingroup\$

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

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

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)
for;i<n;i,z=i+1,N(0){k.Div(z.Mul(k,z.Add(k,N(1))),N(2))}
return k}

Attempt This Online!

\$\endgroup\$
0
\$\begingroup\$

Uiua, 9 bytes

⍥(/++1⇡).

Try it!

\$\endgroup\$
1
2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.