Friday 27 February 2015

Remainder on division - comments on the logic

In my previous post I presented some logic for obtaining the remainder when a number like 4593 is divided by 7. I now want to go through how this logic works.

Some initial values are first stored in memory locations B, C and D and the use of these memories is explained as we go along. The divisor (in our example 7) is input in memory A and remains fixed. Memory D contains the common factor of 10 in our process and the first while loop finds the remainder when 10 is divided by 7 and the result, 3, is stored in D.

The first digit of our number (in our example this is 3) is input after label 1. Essentially this is the main loop of our program. The digits of 4593 are entered reading from right to left and when the last digit has been read, the process is terminated by entering -1 in X.

Each digit is processed as follows. Firstly, the initial while loop determines the remainder on dividing X by A. For the first digit in our example this is the remainder on dividing 3 by 7 (which is 3). Each time we process a new digit of our number 4593 we add to memory C the product of X (the remainder on dividing our digit by 7) and the remainder on dividing the power of 10 by 7 stored in B. For example, for the third digit 5, B at this stage would contain the remainder on dividing 100 by 7, namely 2. We then calculate the remainder on dividing C by A in the following while loop.

Finally, before going on to input the next digit we calculate the remainder on dividing the next power of 10 by A.

When the last digit has been processed, the required remainder in C is displayed.

No comments:

Post a Comment