We are currently working on new rules for what content should and shouldn't be allowed on this website, and are looking for feedback! See Esolang:2026 topicality proposal to view and give feedback on the current draft.

Triangular numbers

From Esolang
Jump to navigation Jump to search

Triangular numbers are numbers that are the counts of objects arranged in an equilateral triangle. It can be calculated as , , or . The sequence is as such:

0, 1, 3, 6, 10, 15, 21, 28, 36, 45, 55, 66, 78, 91, 105...

Visualized, that is:

            +0 = 0
     o      +1 = 1
    o o     +2 = 3
   o o o    +3 = 6
  o o o o   +4 = 10
 o o o o o  +5 = 15
o o o o o o +6 = 21 = 6(6+1)/2
                    = 6(7  )/2
                    = 42    /2
                    = 21

Implementations

AEL

40?0 0 2d[$1+!+!]m#

Counting

Outputs in unary.

when cnt == 0
  read acc
when cnt <= acc
  out "*" * cnt
when cnt > acc
  halt
   
% 1 + 2 + ... + acc

DeBruijn

Uses the fixed-point "Y" combinator to recursively find the sum of the first n naturals. The definition of f is used as example input.

(F \\0 )
(T \\1 )
(S \\\1(210) )
(f S(S(S(S(SF)))) )
(P \\\021 )
(- \0(\(\P(1F)(S(1F)))\0)(PFF)T )
(Z \0(\0FF)T )
(Y \(\1(00))[\1(00)] )

Y\\Z0[F][1(-0)S0]
> debruijn example.txt f
rem: λ λ 1 (1 (1 (1 (1 (1 (1 (1 (1 (1 (1 (1 (1 (1 (1 0))))))))))))))

Free2Edit

+5+5wsr1onl1r2+1l2r1+cl1ocwe

Iterate

(*)∞<
  *n<
    *~n<
      (1*)<>
    >
    !
  >
  (4*)1<
    (2*)=1<
      *~n< &2 >
      @
      !4
    >
    (3*)48<
      *~n< &3 >
      ~@
    >
  >
  (5*)10<
    *~n< &5 >
    ~@
  >
>

Swordfish

1~[0,,,,,v
]+#~500$%\
1]+~[@,,,\