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

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Egyptian division step by step in the Ring 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 Ring programming language

Source code in the ring programming language

load "stdlib.ring"

table = newlist(32, 2)
dividend = 580
divisor = 34
 
i = 1
table[i][1] = 1
table[i][2] = divisor
 
while table[i] [2] < dividend
      i = i + 1
      table[i][1] = table[i -1] [1] * 2
      table[i][2] = table[i -1] [2] * 2
end 
i = i - 1
answer = table[i][1]
accumulator = table[i][2]
 
while i > 1
      i = i - 1
      if table[i][2]+ accumulator <= dividend 
         answer = answer + table[i][1]
         accumulator = accumulator + table[i][2]
      ok
end
 
see string(dividend)  + " divided by " + string(divisor) + " using egytian division" + nl
see " returns " + string(answer) + " mod(ulus) " + string(dividend-accumulator)

  

You may also check:How to resolve the algorithm Long primes step by step in the M2000 Interpreter programming language
You may also check:How to resolve the algorithm Summarize primes step by step in the C++ programming language
You may also check:How to resolve the algorithm Sort an array of composite structures step by step in the Julia programming language
You may also check:How to resolve the algorithm Hello world/Graphical step by step in the ATS programming language
You may also check:How to resolve the algorithm One of n lines in a file step by step in the PureBasic programming language