25
\$\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\$ 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\$ Jul 13, 2023 at 18:24
  • \$\begingroup\$ @JonathanAllan Oh yeah it’s Vyxal R \$\endgroup\$ Jul 13, 2023 at 19:00

41 Answers 41

11
\$\begingroup\$

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
\$\endgroup\$
1
  • 4
    \$\begingroup\$ Gotta go with the flo(w)! \$\endgroup\$
    – Neil
    Jul 11, 2023 at 20:27
11
\$\begingroup\$

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!

\$\endgroup\$
1
  • 2
    \$\begingroup\$ 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. \$\endgroup\$ Jul 12, 2023 at 7:31
10
\$\begingroup\$

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!

\$\endgroup\$
1
  • 1
    \$\begingroup\$ Using xnor's calculation I can get 40 bytes, but py3.8+ only. \$\endgroup\$
    – anderium
    Jul 11, 2023 at 22:40
6
\$\begingroup\$

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 
\$\endgroup\$
5
\$\begingroup\$

><> (Fish), 23 bytes

i::?\~n;
-10.\r:1+*2,r1

Try it

enter image description here

\$\endgroup\$
5
\$\begingroup\$

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
]>
\$\endgroup\$
4
\$\begingroup\$

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

\$\endgroup\$
4
\$\begingroup\$

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.

\$\endgroup\$
4
\$\begingroup\$

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 
\$\endgroup\$
4
\$\begingroup\$

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.

\$\endgroup\$
2
  • 1
    \$\begingroup\$ For the second solution, assigning the two-argument recursive function to ? instead of f is one byte shorter, as usual :) \$\endgroup\$
    – pajonk
    Jul 12, 2023 at 5:54
  • \$\begingroup\$ 32 bytes, no <<- needed! \$\endgroup\$
    – Giuseppe
    Jul 19, 2023 at 1:51
4
\$\begingroup\$

Ruby, 34 bytes

f=->x,y=x{y<1?x:f[(1..x).sum,y-1]}

Try it online!

\$\endgroup\$
4
\$\begingroup\$

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="

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

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)
\$\endgroup\$
1
  • 1
    \$\begingroup\$ This can be (⊢+´↕)⍟⊣ (left argument of reduction is seed value), which can be further shortened to +´⟜↕⍟⊣. \$\endgroup\$
    – ovs
    Jul 11, 2023 at 21:35
4
\$\begingroup\$

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
\$\endgroup\$
2
  • \$\begingroup\$ (-:*>:)^:] works with extended precision integers. \$\endgroup\$
    – Bubbler
    Jul 12, 2023 at 10:14
  • \$\begingroup\$ Ah, good catch! \$\endgroup\$
    – south
    Jul 12, 2023 at 18:01
3
\$\begingroup\$

JavaScript (Node.js), 29 bytes

f=(x,y=x)=>y?f(x*++x/2,y-1):x

Try it online!

\$\endgroup\$
3
\$\begingroup\$

Husk, 5 bytes

!→¹¡Σ

Try it online!

\$\endgroup\$
3
\$\begingroup\$

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.

\$\endgroup\$
3
\$\begingroup\$

Charcoal, 13 bytes

NθFθ≔Σ…·¹θθIθ

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

Nθ

Input n.

Fθ

Repeat n times...

≔Σ…·¹θθ

... replace the current value the sum of its range.

Iθ

Output the final value.

14 bytes for an efficient version:

NθFθ≔÷×⊕θθ²θIθ

Try it online! Link is to verbose version of code. Explanation: Uses the triangle formula to sum the range.

\$\endgroup\$
3
\$\begingroup\$

Vyxal R, 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)
\$\endgroup\$
8
  • \$\begingroup\$ uhm ackshully, this can be shorter if you use vyncode, a fractional byte compression scheme first introduced in vyxal 2.21.0🤓 \$\endgroup\$
    – lyxal
    Jul 12, 2023 at 5:34
  • 1
    \$\begingroup\$ At least it’s >0 \$\endgroup\$ Jul 12, 2023 at 7:36
  • \$\begingroup\$ exactly what I was thinking of \$\endgroup\$ Jul 12, 2023 at 10:48
  • \$\begingroup\$ Why is this not titled "vyncode -R", or perhaps "Vyxal -R="? (I'm not familiar with the implementation). \$\endgroup\$ Jul 13, 2023 at 19:08
  • \$\begingroup\$ @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. \$\endgroup\$
    – The Thonnu
    Jul 13, 2023 at 19:12
2
\$\begingroup\$

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

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

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)
\$\endgroup\$
2
\$\begingroup\$

Nekomata, 4 bytes

ᵑ{R∑

Attempt This Online!

ᵑ{R∑
ᵑ{      Repeat
  R     Range
   ∑    Sum
\$\endgroup\$
2
\$\begingroup\$

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.

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

Brachylog, 8 bytes

;?{⟦+}ⁱ⁾

Try it online!

Explanation

;?{  }ⁱ⁾         Iterate <Input> times on the Input:
   ⟦               Range
    +              Sum
\$\endgroup\$
2
\$\begingroup\$

Haskell, 29 bytes

(!!)=<<iterate(\x->sum[0..x])

Expands to

\n -> iterate (\x -> sum [0..x]) n !! n

Try it online!

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

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
\$\endgroup\$
2
\$\begingroup\$

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
\$\endgroup\$
2
\$\begingroup\$

Raku, 25 bytes

{($_,{.++*$_/2}...*)[$_]}

Try it online!

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

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

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

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

\$\endgroup\$

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.