-2
$\begingroup$

In an algorithm book once the first example was how to compute a multiplication in a loop (only that, so I just remembered, and wanted to do it programmatically but with all operations)

Multiplication was simple, say 10 * 4:

base = 0, x = 10, y = 4:

While y != 0:
base + x = 10, y--
base + x = 20, y--
base + x = 30, y--
base + x = 40, y-- (y is now 0)

Result of base: 40 (correct)

As the opposite of taking away is giving, I assumed the same was for division and multiplication (doubling, halving), but: "base - x.." clearly does not give anything useful in an iteration..

Is this impossible? Am I looking at an algorithm to divide completely the wrong way (not like my mult)?

$\endgroup$
2
  • $\begingroup$ Take away x until you would go below zero. The number of times you took x away is the quotient. The number left is the remainder. Is this what you are looking for? Or do you want a decimal expansion of the fraction you get? By the way, you cannot multiply fractions using your algorithm - what is 1/2 times 1/3? Add 1/3 to itself repeatedly 1/2 a time? A computer would no take that instruction. $\endgroup$ Commented Sep 12, 2010 at 20:09
  • 4
    $\begingroup$ Have you read en.wikipedia.org/wiki/Multiplication_algorithms ? $\endgroup$ Commented Sep 12, 2010 at 20:23

1 Answer 1

0
$\begingroup$

Division is repeated subtraction. Take 11 \div 3 as an example.

11 - 3 = 8. Increment the quotient by 1. Since 8 > 3, keep going.

8 - 3 = 5. Increment the quotient by 1. Since 5 > 3, keep going.

5 - 3 = 2. Increment the quotient by 1. Since 2 < 3, set the remainder to 2 and terminate.

I'll leave it to you to figure out the code.

$\endgroup$

Not the answer you're looking for? Browse other questions tagged .