RosettaCodeData/Task/Egyptian-division/Mathematica/egyptian-division.math
2023-07-01 13:44:08 -04:00

22 lines
475 B
Text

ClearAll[EgyptianDivide]
EgyptianDivide[dividend_, divisor_] := Module[{table, i, answer, accumulator},
table = {{1, divisor}};
i = 1;
While[Last[Last[table]] < dividend,
AppendTo[table, 2^i {1, divisor}];
i++
];
table //= Most;
answer = 0;
accumulator = 0;
Do[
If[accumulator + t[[2]] <= dividend,
accumulator += t[[2]];
answer += t[[1]]
]
,
{t, Reverse@table}
];
{answer, dividend - accumulator}
]
EgyptianDivide[580, 34]