Triangular numbers

From Esolang
(Redirected from Triangle numbers)
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 by computing n(n+1)/2 or 1 + 2 + ... + n. The sequence is as such:

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

Visualized, that is (starting from 1):

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

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]+~[@,,,\