Human Resource Machine/Year 24

Strategy
The modulus operation is usually unfamiliar to people who aren't programmers or mathematicians. As the description explains, the modulus of two numbers is the remainder left over when you divide two whole numbers. Say you want to divide 11 by 3, for example. 3 goes into 11 three times, with a remainder of 2. And if for example, you try dividing 15 by 5, there is no remainder, so 15 modulus 5 is 0.

As you can tell, you have no DIVIDE command just like you don't have a MULTIPLY command. But that's fine. Just as you were able to perform multiplication with ADD commands, you can do division with SUB commands. Take the 11 divided by 3 example. In order to determine what the remainder is, we can continue to subtract 3 from 11, until we're left with some number smaller than 3. When we're down to a value that's less than 3, that's the remainder.

So we'll start by grabbing the first number and putting it in box 0. This is the number that's going to be divided. Then we'll grab the second number and put it in box 1. That's the number to divide by.

To do the math correctly, we want to pick the number in box 0 up, and then subtract the number box 1 from it. We want to do this continuously until we reach a negative number. Once we hit a negative number, we know we've subtracted one too many times, so we'll add the contents of box 1 back to undo that, and then copy the answer to the outbox. Your program may resemble the following:

a:   INBOX COPYTO  0 INBOX COPYTO  1 COPYFROM 0 b:   SUB      1 JUMPN   c    JUMP     b c:    ADD      1 OUTBOX JUMP    a

Optimizing
If you wrote a program like the one above, you may notice you've already met the size and speed challenges. However, we can always continue to optimize the performance of the program. In this case, the simple trick of moving the ADD and OUTBOX commands above the first INBOX command and jumping over them the first time will give you a small speed boost:

JUMP    b a:    ADD      1 OUTBOX b:   INBOX COPYTO  0 INBOX COPYTO  1 COPYFROM 0 c:   SUB      1 JUMPN   a    JUMP     c