How to resolve the algorithm Egyptian division step by step in the XPL0 programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Egyptian division step by step in the XPL0 programming language

Table of Contents

Problem Statement

Egyptian division is a method of dividing integers using addition and doubling that is similar to the algorithm of Ethiopian multiplication Algorithm: Given two numbers where the dividend is to be divided by the divisor:

Example: 580 / 34 Table creation: Initialization of sums: Considering table rows, bottom-up: When a row is considered it is shown crossed out if it is not accumulated, or bold if the row causes summations. So 580 divided by 34 using the Egyptian method is 17 remainder (578 - 580) or 2.

The task is to create a function that does Egyptian division. The function should closely follow the description above in using a list/array of powers of two, and another of doublings.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Egyptian division step by step in the XPL0 programming language

Source code in the xpl0 programming language

include xpllib; \for Print

proc EgyptianDivide(Dividend, Divisor, AddrQuotient, AddrRemainder);
int  Dividend, Divisor, AddrQuotient, AddrRemainder;
int  PowersOfTwo(100), Doublings(100), Doubling, Accumulator, I;
[if Dividend < Divisor then
        [AddrQuotient(0):= 0;  AddrRemainder(0):= Dividend;  return];
PowersOfTwo(0):= 1;  Doublings(0):= Divisor;  Doubling:= Divisor;  I:= 1;
loop    [Doubling:= Doubling*2;
        if Doubling > Dividend then quit;
        PowersOfTwo(I):= PowersOfTwo(I-1)*2;
        Doublings(I):= Doubling;
        I:= I+1;
        ];
AddrQuotient(0):= 0;  Accumulator:= 0;
for I:= I-1 downto 0 do
    [if Accumulator + Doublings(I) <= Dividend then
        [Accumulator:= Accumulator + Doublings(I);
        AddrQuotient(0):= AddrQuotient(0) + PowersOfTwo(I);
        if Accumulator = Dividend then I:= 0;
        ];
    ];
AddrRemainder(0):= Dividend - Accumulator;
];

int  Dividend, Divisor, Quotient, Remainder;
[Dividend:= 580;  Divisor:= 34;
EgyptianDivide(Dividend, Divisor, @Quotient, @Remainder);
Print("%d / %d = %d with remainder %d.\n", Dividend, Divisor, Quotient, Remainder);
]

  

You may also check:How to resolve the algorithm File input/output step by step in the ALGOL 68 programming language
You may also check:How to resolve the algorithm Percentage difference between images step by step in the Wren programming language
You may also check:How to resolve the algorithm Multi-dimensional array step by step in the Lingo programming language
You may also check:How to resolve the algorithm Read entire file step by step in the Ada programming language
You may also check:How to resolve the algorithm Towers of Hanoi step by step in the UNIX Shell programming language