# Go deh!

Mainly Tech projects on Python and Electronic Design Automation.

# Egyptian division

Egyptian division is a method of dividing integers using addition and doubling that is similar to the algorithm of Ethiopian multiplicaion

## Algorithm:

Given two numbers where the dividend is to be divided by the divisor:
1. Start the construction of a table of two columns: `powers_of_2`, and `doublings`; by a first row of a 1 (i.e. 2^0) in the first column and 1 times the divisor in the first row second column.
2. Create the second row with columns of 2 (i.e 2^1), and 2 * divisor in order.
3. Continue with successive i'th rows of 2^i and 2^i * divisor.
4. Stop adding rows, and keep only those rows, where 2^i * divisor is less than or equal to the dividend.
5. We now assemble two separate sums that both start as zero, called here answer and accumulator
6. Consider each row of the table, in the reverse order of its construction.
7. If the current value of the accumulator added to the doublings cell would be less than or equal to the dividend then add it to the accumulator, as well as adding the powers_of_2 cell value to the answer.
8. When the first row has been considered as above, then the integer division of dividend by divisor is given by answer.
(And the remainder is given by the absolute value of accumulator - dividend).

## Example: 580 / 34

### Table creation:

powers_of_2doublings
134
268
4136
8272
16544

134
268
4136
8272
16544
00

### 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.
134
268
4136
8272
1654416544
134
268
4136
827216544
16544
134
268
413616544
8272
16544
134
26816544
4136
8272
16544
13417578
268
4136
8272
16544

### Finally

So 580 divided by 34 using the Egyptian method is `17` remainder (578 - 580) or `2`.

# Rosetta Code

This blog post should be a Rosetta Code task, but at the time of writing the site has been down for maintenance for a week. I will still writethe task description and its first Python solution though.

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.
• Functions should be clear interpretations of the algorithm.
• Use the function to divide 580 by 34 and show the answer here, on this page.

# Python Solution

In [2]:
```def egyptian_divmod(dividend, divisor):
assert divisor != 0
pwrs, dbls = [1], [divisor]
while dbls[-1] <= dividend:
pwrs.append(pwrs[-1] * 2)
dbls.append(pwrs[-1] * divisor)
ans, accum = 0, 0
for pwr, dbl in zip(pwrs[-2::-1], dbls[-2::-1]):
if accum + dbl <= dividend:
accum += dbl
ans += pwr
return ans, abs(accum - dividend)

# Test it gives the same results as the divmod built-in
from itertools import product
for i, j in product(range(13), range(1, 13)):
assert egyptian_divmod(i, j) == divmod(i, j)

# Mandated result
i, j = 580, 34
print(f'{i} divided by {j} using the Egyption method is %i remainder %i'
% egyptian_divmod(i, j))
```
```580 divided by 34 using the Egyption method is 17 remainder 2
```

# Extra

A quick note on hardware applications.
The astute may note that the doublings column entries are easily computed as successive shifts of the binary representation of the divisor; and that the result in binary can be read off by marking 1 if a table row is summed or 0 if it is not.
The text passage describes the Egyptian division method, an algorithm for dividing integers using addition and doubling. It includes an example of dividing 580 by 34, and provides a Python implementation of the algorithm.
single_question_with_answer

1. the passage contains a mathematical question: "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. use the function to divide 580 by 34 and show the answer here, on this page."
2. the answer to this question is provided in the python code output: "580 divided by 34 using the egyption method is 17 remainder 2."
Given a text passage, determine:
    1. If it contains a mathematical question (including word problems or calculations)
    2. If an answer to that mathematical question is provided in the text

First, describe the content structure you found in 1-2 sentences.

Then provide your analysis in the following format:
TYPE: <one of the following values>
- NO_QUESTION
- SINGLE_QUESTION
- SINGLE_QUESTION_WITH_ANSWER
- MULTIPLE_QUESTIONS
- MULTIPLE_QUESTIONS_WITH_ANSWERS
system
# Go deh!

Mainly Tech projects on Python and Electronic Design Automation.

# Egyptian division

Egyptian division is a method of dividing integers using addition and doubling that is similar to the algorithm of Ethiopian multiplicaion

## Algorithm:

Given two numbers where the dividend is to be divided by the divisor:
1. Start the construction of a table of two columns: `powers_of_2`, and `doublings`; by a first row of a 1 (i.e. 2^0) in the first column and 1 times the divisor in the first row second column.
2. Create the second row with columns of 2 (i.e 2^1), and 2 * divisor in order.
3. Continue with successive i'th rows of 2^i and 2^i * divisor.
4. Stop adding rows, and keep only those rows, where 2^i * divisor is less than or equal to the dividend.
5. We now assemble two separate sums that both start as zero, called here answer and accumulator
6. Consider each row of the table, in the reverse order of its construction.
7. If the current value of the accumulator added to the doublings cell would be less than or equal to the dividend then add it to the accumulator, as well as adding the powers_of_2 cell value to the answer.
8. When the first row has been considered as above, then the integer division of dividend by divisor is given by answer.
(And the remainder is given by the absolute value of accumulator - dividend).

## Example: 580 / 34

### Table creation:

powers_of_2doublings
134
268
4136
8272
16544

134
268
4136
8272
16544
00

### 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.
134
268
4136
8272
1654416544
134
268
4136
827216544
16544
134
268
413616544
8272
16544
134
26816544
4136
8272
16544
13417578
268
4136
8272
16544

### Finally

So 580 divided by 34 using the Egyptian method is `17` remainder (578 - 580) or `2`.

# Rosetta Code

This blog post should be a Rosetta Code task, but at the time of writing the site has been down for maintenance for a week. I will still writethe task description and its first Python solution though.

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.
• Functions should be clear interpretations of the algorithm.
• Use the function to divide 580 by 34 and show the answer here, on this page.

# Python Solution

In [2]:
```def egyptian_divmod(dividend, divisor):
assert divisor != 0
pwrs, dbls = [1], [divisor]
while dbls[-1] <= dividend:
pwrs.append(pwrs[-1] * 2)
dbls.append(pwrs[-1] * divisor)
ans, accum = 0, 0
for pwr, dbl in zip(pwrs[-2::-1], dbls[-2::-1]):
if accum + dbl <= dividend:
accum += dbl
ans += pwr
return ans, abs(accum - dividend)

# Test it gives the same results as the divmod built-in
from itertools import product
for i, j in product(range(13), range(1, 13)):
assert egyptian_divmod(i, j) == divmod(i, j)

# Mandated result
i, j = 580, 34
print(f'{i} divided by {j} using the Egyption method is %i remainder %i'
% egyptian_divmod(i, j))
```
```580 divided by 34 using the Egyption method is 17 remainder 2
```

# Extra

A quick note on hardware applications.
The astute may note that the doublings column entries are easily computed as successive shifts of the binary representation of the divisor; and that the result in binary can be read off by marking 1 if a table row is summed or 0 if it is not.
user
The text passage describes the Egyptian division method, an algorithm for dividing integers using addition and doubling. It includes an example of dividing 580 by 34, and provides a Python implementation of the algorithm.

TYPE: SINGLE_QUESTION_WITH_ANSWER

1. The passage contains a mathematical question: "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. Use the function to divide 580 by 34 and show the answer here, on this page."
2. The answer to this question is provided in the Python code output: "580 divided by 34 using the Egyption method is 17 remainder 2."