(int a, int b)
| 27 | |
| 28 | /* Multiply a by b by adding a to itself b times */ |
| 29 | public static int multiply(int a, int b) { |
| 30 | if (a < b) { |
| 31 | return multiply(b, a); // algo is faster if b < a |
| 32 | } |
| 33 | int sum = 0; |
| 34 | for (int i = abs(b); i > 0; i--) { |
| 35 | sum += a; |
| 36 | } |
| 37 | if (b < 0) { |
| 38 | sum = negate(sum); |
| 39 | } |
| 40 | return sum; |
| 41 | } |
| 42 | |
| 43 | /* Divide a by b by literally counting how many times b can go into |
| 44 | * a. That is, count how many times you can add b to itself until you reach a. */ |