| Bytes | Lang | Time | Link |
|---|---|---|---|
| 023 | TIBASIC TI84 Plus CE Python | 250912T174522Z | madeforl |
| 019 | Juby | 230711T192807Z | Jordan |
| 043 | Setanta | 240720T235346Z | bb94 |
| 009 | Uiua | 231221T022932Z | chunes |
| 015 | ForWhile | 230711T202018Z | bsoelch |
| 045 | Desmos | 230816T195539Z | fwoosh |
| 036 | C gcc | 230718T213356Z | Saladin |
| 020 | Alice | 230711T211003Z | Julian |
| 026 | Julia 1.0 | 230712T151026Z | Ashlin H |
| 025 | Raku | 230717T214406Z | bb94 |
| 027 | Perl 5 | 230717T131954Z | Nahuel F |
| 010 | Dyalog APL | 230711T205320Z | RubenVer |
| 056 | Go | 230713T180032Z | bigyihsu |
| 073 | sed | 230712T201806Z | Yelp |
| 017 | x8664 machine code | 230712T195709Z | engineer |
| 029 | Haskell | 230712T181405Z | Magma |
| 010 | J | 230712T071648Z | south |
| 068 | Python + vyxal | 230712T110922Z | The Empt |
| 006 | Japt h | 230712T104827Z | Shaggy |
| 008 | Brachylog | 230712T093015Z | Fatalize |
| 035 | Python 2 | 230711T204554Z | xnor |
| 020 | Wolfram Language Mathematica | 230712T074226Z | JSorngar |
| 048 | Brainfuck | 230711T230720Z | anderium |
| 004 | MathGolf | 230712T065703Z | Kevin Cr |
| 175 | Vyxal R | 230711T200413Z | The Thon |
| 006 | BQN | 230711T200456Z | Dominic |
| 004 | Nekomata | 230712T000118Z | alephalp |
| 062 | Python 3 | 230711T183737Z | Ethan C |
| 034 | Ruby | 230711T210949Z | G B |
| 013 | Charcoal | 230711T202638Z | Neil |
| 018 | Retina | 230711T202216Z | Neil |
| 033 | R≥4.1 | 230711T201918Z | Dominic |
| 005 | MATL | 230711T201857Z | Luis Men |
| 003 | 05AB1E | 230711T200844Z | The Thon |
| 052 | Racket – | 230711T200651Z | Ed The & |
| 003 | Thunno 2 | 230711T200235Z | The Thon |
| 026 | Factor + math.unicode | 230711T194536Z | chunes |
| 006 | Pyth | 230711T193538Z | CursorCo |
| 042 | Python 3 | 230711T190906Z | spooky_s |
| 005 | Husk | 230711T183531Z | Dominic |
| 023 | ><> Fish | 230711T181551Z | mousetai |
| 029 | JavaScript Node.js | 230711T175525Z | l4m2 |
| 004 | Jelly | 230711T174710Z | caird co |
TI-BASIC (TI-84 Plus CE Python), 23 bytes
Input A
For(N,1,A
.5(A+A²→A
End
A
the sequence basically is $$ a(0)=n\\ a(k)=\frac{a(k-1)+a(k-1)^2}{2} $$ while \$k \le n\$ (take it with a grain of salt i'm not good with this stuff)
J-uby, 19 bytes
~:^%(:**&(:+|:sum))
Explanation
With some whitespace:
~:^ % (:** & (:+ | :sum))
The core function here is :** & (:+ | :sum), where :+ constructs a range and :sum sums it, and :** applies that function to its own result n times. The rest is just argument shuffling.
ForWhile, 15 bytes
{:(.0'(+:):)}1$
same length, but faster (uses formula for sum of first n numbers):
{:(.::*+2/:)}1$
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.
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
Explaination:
Applies \$f\to T(f)\$ n times, using a ticker because Desmos does not support recursion.
C (gcc), 36 bytes
x;a(n){for(x=n;n--;x/=2)x+=x*x;n=x;}
Was missing a semicolon, so now it's 36 bytes.
Alice, 24 20 bytes
/O
\I@/.!wrd&+?t.!$K
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
Does not time out as it uses the n*(n+1)/2 formula instead
Julia 1.0, 33 26 bytes
~x=(1:x.|>_->x*=(x+1)/2;x)
-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)...)
-4 bytes thanks to MarcMush: remove call to reduce
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
Go, 56 bytes
func(n int)int{k,i:=n,0
for;i<n;i++{k=k*-^k/2}
return k}
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}
sed, 73 bytes
s/!*/&''&/g
:a
s/<(!*)''!(!*>)/<'\1'\2/
s/(<!*)'(!*)!/\1\2!'\2/
ta
s/'//g
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.
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
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
Haskell, 29 bytes
(!!)=<<iterate(\x->sum[0..x])
Expands to
\n -> iterate (\x -> sum [0..x]) n !! n
J, 11 10 bytes
(-:*>:)^:]
Requires extended precision integer as input. -1 thanks to @Bubbler.
(-:*>:)^:]
^: NB. repeat
] NB. y times
>: NB. increment
-: NB. halve
* NB. multiply
J, 12 11 bytes
(-:*>:)^:x:
(-:*>:)^:]
^: NB. repeat
x: NB. y times but promote to extended precision
>: NB. increment
-: NB. halve
* NB. multiply
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
Japt -h, 6 bytes
õ@=ô x
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
Python 2, 35 bytes
def f(n):exec"n=n*-~n/2;"*n;print n
A function that prints.
Python 3, 39 bytes
lambda n:eval("(n:=n*-~n//2)*0+"*n+"n")
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]
Here were some other attempts.
39 bytes
lambda n:eval('('*n+'n'+'+.5)**2//2'*n)
Based on @dingledooper's comment.
40 bytes
def f(n):exec("n*=n/2+.5;"*n+"print(n)")
41 bytes
lambda n:0*[n:=n*-~n//2for _ in[1]*n]or n
Wolfram Language (Mathematica), 20 bytes
Nest[#(#+1)/2&,#,#]&
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.
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
]>
MathGolf, 6 4 bytes
_Å╒Σ
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:
- MathGolf uses
0if a stack is empty and it needs an integer. Although I think it's a bug in MathGolf (I'm not 100% sure), unfortunately that0will take priority over the implicit input-integer in the first iteration of a for-loop. So for any \$n>0\$, justÅ╒Σwill result in0becauseÅwill push a 0 in its first iteration and use it for the╒, instead of using the (implicit) input-integer. - 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 second0is still on the stack to be output instead.
Vyxal R, 14 bitsv2, 1.75 bytes
(∑
Yeah...
( # Input number of times:
∑ # Sum top of stack
# (R flag makes it take the range instead of the digits)
BQN, 9 6 bytes
Edit: -3 bytes thanks to ovs
+´⟜↕⍟⊣
+´⟜↕⍟⊣
⍟ # 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)
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)
Calculates a(n) using the OEIS definition given in the OP, probably still golfable
Edit: fixed inputs and added "f="
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.
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.
R≥4.1, 33 bytes
\(x){Map(\(i)x<<-sum(0:x),1:x);x}
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)
Without the short \ form, it's too expensive to define two functions.
MATL, 5 bytes
t:":s
How it works
t % Input (implicit). Duplicate
: % Range
" % For each
: % Range
s % Sum
% End (implicit). Display (implicit)
05AB1E, 3 bytes
FLO
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
Racket – 52 bytes
(define(f x y)(if(= y 0)x(f(/(*(+ x 1)x)2)(- y 1))))
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.
Thunno 2, 3 bytes
{RS
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
Factor + math.unicode, 26 bytes
[ dup [ [1,b] Σ ] times ]
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.
Pyth, 6 bytes
LsSbyF
Alternative 6 byte solution
usSGQQ
Another alternative 6 byte solution
.v*"sS
This one's my favorite.
Another alternative 6 byte solution
em=sSQ
Feels like there's gotta be a 5 byte solution but I got nothing
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)
Jelly, 4 bytes
RS$¡
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
