Try copying and pasting the following programs into the editor in order to get a feeling on how these LOOP and WHILE programs work.
The examples are ordered by name (alphabetically) and the result of the program is always given by x0.
x4 = x2 + 0;
x3 = x1 - x2;
Loop x3 Do
x4 = x1 + 0
End;
x5 = x2 + 0;
x3 = x2 - x1;
Loop x3 Do
x5 = x1 + 0
End;
x0 = x4 - x5
Given two natural numbers x1, x2, this program calculates their absolute difference.
x0 = abs(x1 - x2)
While x1 > 0 Do
x2 = x2 + 1;
x1 = x1 - 1
End;
x0 = x2 + 0
Given two natural numbers x1, x2, this program calculates their sum.
x0 = x1 + x2
x2 = 0 + 0;
x0 = 1 + 0;
While x1 > 0 Do
x4 = x0 + 0;
x0 = x2 + x0;
x2 = x4 + 0;
x1 = x1 - 1
End
Given a natural number x1, this program calculates the (n-1)th fibonacci number.
x1 = 1, x0 = 1
x1 = 2, x0 = 2
x1 = 3, x0 = 3
x1 = 4, x0 = 5
x1 = 5, x0 = 8
x1 = 6, x0 = 13
x1 = 7, x0 = 21
etc.
While x2 > 0 Do
x4 = x2 + 0;
x0 = x1 + 0;
Loop x1 Do
x1 = x1 - x2;
x3 = x1 + 0;
x3 = 1 - x3;
x3 = 1 - x3;
Loop x3 Do
x0 = x1 + 0
End
End;
x1 = x2 - 1;
x1 = x0 - x1;
Loop x1 Do
x0 = 0 + 0
End;
x2 = x0 + 0;
x1 = x4 + 0
End;
x0 = x1 + 0
Given two natural numbers x1, x2, this program calculates their greatest common divisor.
x0 = gcd (x1, x2)
The result is computed by the euclidean algorithm.
x3 = x1 - x2;
x3 = x3 + 1;
While x3 > 0 Do
x3 = x3 - x2;
x0 = x0 + 1
End
Given two natural numbers x1 (dividend), x2 (divisor, bigger than 0), this program returns the quotient x1/x2, i. e the biggest natural number x0 such that x1 = x0*x2 + r, where r is smaller than the divisor x2
x0 = x1 / x2 + remainder
where remainder < x2
Hint: The remainder r can be calculated by x1 mod x2.
x0 = x2 + 0;
x3 = x1 - x2;
Loop x3 Do
x0 = x1 + 0
End
Given two natural numbers x1, x2, this program returns the bigger one.
x0 = max(x1, x2)
x0 = x2 + 0;
x3 = x2 - x1;
Loop x3 Do
x0 = x1 + 0
End
Given two natural numbers x1, x2, this program returns the smaller one.
x0 = min(x1, x2)
x0 = x1 + 0;
Loop x1 Do
x1 = x1 - x2;
x3 = x1 + 0;
x3 = 1 - x3;
x3 = 1 - x3;
Loop x3 Do
x0 = x1 + 0
End
End;
x1 = x2 - 1;
x1 = x0 - x1;
Loop x1 Do
x0 = 0 + 0
End
Given two natural numbers x1, x2, this program calculates x1 modulo x2.
x0 = x1 mod x2
Loop x1 Do
x0 = x0 + x2
End
Given two natural numbers x1, x2, this program calculates their product.
x0 = x1 * x2