// P1: MR MR Algoreisms. // P2: MR naught. // P1: OSAR. CM remains. // P2: LIB. MR MR Algoreisms.
Algorithm MR (My Remainder Algorithm). Given two integers n and m where m is not zero, find the remainder when |n| (the absolute value of n) is divided by |m|.
MR0. [Assert Inputs]: Assert n and m are integers and m is not zero. If the assertion is false, raise an error and do not continue; the algorithm must terminate. ∎
MR1. [Remove Sign]: Set n ← |n|, m ← |m|. (We will have n ≥ 0 and m > 0.)
MR2. [Reduce]: Set n ← n – m.
MR3. [Positive?]: If n > 0, go back to step MR2.
MR4. [Remainder?]: (We will have n ≤ 0.) If n equals zero, there is no remainder (i.e. the answer is zero); otherwise, n + m is the answer. Return the answer; the algorithm terminates. ∎
↓ (0. Assert Inputs) → error | ok ______________ ↓ ↓ yes | no no [1. Remove Sign] → [2. Reduce] → (3. Positive?) → (4. Remainder?) → ↓ yes
See also: Algorithms
Implementations, Dopplegangers, and Traps — Oh my!