Iterate/Math

From Esolang
Jump to navigation Jump to search

Back to Iterate

Arithmetic

A + B

*A< (1*)<> > // increment L1
*B< (1*)<> > // increment L1 again

A - B

Note: Results lower than one will not output anything.

*A< (1*)<> > // L1 = A
*B<          // decrement L1 by one B times
  $2
  *=1<            // store L1-1 in L2
    *~n< (2*)<> > // ~n acts as L1-1
    !
  >
  $1
  *=2< (1*)<> >   // copy from L2 to L1
>

A * B

*A<
  *B<
    (1*)<>   // visited A*B times as an intrinsic property of nested loops
  >
>

A % B

*A< (1*)<> >        // L1 = A
*B< (2*)<> (4*)<> > // L2 = B, L4 = B
(6*)=1<             // increment L3 to equal L1
  (3*)<>            // L3 = L3 + 1
  $5
  *=4< (5*)<> >     // L5 = L4
  $4
  *=5<           
    *~n< (4*)<> >   // L4 = L5 - 1
    !
  >
  *=4< &6 >         // if L4 == 0 (or when modulus returns 0)...
  $3                // L3 = 0
  *=2< (4*)<> >     // L4 = L2 (reset count)
>

A / B (floored)

The same algorithm can be used to calculate floored division.

*A< (1*)<> >
*B< (2*)<> (4*)<> >
(6*)=1<         // a%b algorithm
  (3*)<>
  $5
  *=4< (5*)<> >
  $4
  *=5<           
    *~n< (4*)<> >
    !
  >
  *=4< &6 >
  $3
  *=2< (4*)<> >
  (7*)<>        // count how many times L3 resets for the quotient
>

A^B

*A< (1*)<> (3*)<> >
*B< *~n< (2*)<> > ! > // B-1
*=2< // calculate A*A a bunch
  $4
  *=1<
    *=3<
      (4*)<> // L1*L3
    >
  >
  $1
  *=4< (1*)<> > // set L1 to L1*L3
>
(5*)1< // extra case if B is 0
  *=2< !5 >
  $1 (1*)<> // L1 = 1
> 

Floored Bth root of A

A combination of the exponential and conditional algorithms.

*A< (1*)<> >
*B< *~n< (2*)<> > ! >
(3*)=1<           // we'll increment up until we find n^B >= A
  $4 *n< (4*)<> > // initialize n^B
  *=2<            // n^B
    $5
    *=4< *n3< (5*)<> > >
    $4
    *=5< (4*)<> >
  >

  $6 *=1< (6*)<> > // initialize n^B >= A
  (9*)∞<
    (10*)1<
      *=5< *=6< !10 > (11*)<> !9 > // n^B > A
      *=6< *=5< !10 > !9 >         // n^B < A
      (12*)<> !9                   // n^B == A
    >
    $7 $8
    *=5< (7*)<> >
    *=6< (8*)<> >
    $5 $6
    *=7< *~n< (5*)<> > ! >
    *=8< *~n< (6*)<> > ! >
  >

  *=11< *n3< *~n< (13*)<> > ! > !3 > // output n-1 if n > A
  *=12< *n3<      (13*)<>     > !3 > // output n if n == A
>

Logic

Comparisons

While this code checks if A == B, you can trivially modify it to check for A < B, A > B, and any combination or negation of those cases.

*A< (1*)<> > // L1 = A
*B< (2*)<> > // L2 = B
             // L7 will hold if A == B: 1 = true, 0 = false
             // we'll continuously decrement A and B until either reaches 0
             // if either reaches 0 first before the other, they're not equal
             // otherwise they are equal
(5*)∞<
  (6*)1<        // we check FIRST for the edge case where either A or B starts as 0
    *=1<
      *=2< !6 > // case 1: neither A nor B are 0 -> continue
      !5        // case 2: B reached 0 first     -> A > B
    >
    *=2<
      *=1< !6 > // case 1: neither A nor B are 0 -> continue
      !5        // case 3: A reached 0 first     -> A < B
    >
    (7*)<>      // case 4: both reached 0 at the same time -> A == B
    !5
  >
  $3
  $4
  *=1< (3*)<> >        // L3 = L1
  *=2< (4*)<> >        // L4 = L2
  $1
  $2
  *=3< *~n< (1*)<> > ! > // L1 = L3 - 1
  *=4< *~n< (2*)<> > ! > // L2 = L4 - 1
>

NOT A

(1*)<
  *A< !1 >
  (2*)<>
>

A AND B

This is equivalent to multiplication.

*A<
  *B<
    (1*)<>
  >
>

A OR B

This is equivalent to addition.

*A< $1 (1*)<> >
*B< $1 (1*)<> >

A XOR B

This is equivalent to (A AND NOT B) OR (B AND NOT A).

(1*)A< *B< !1 > (3*)<> >
(2*)B< *A< !2 > (3*)<> >