MCPcopy Index your code
hub / github.com/careercup/ctci / multiply

Method multiply

java/Chapter 7/Question7_4/Question.java:29–41  ·  view source on GitHub ↗
(int a, int b)

Source from the content-addressed store, hash-verified

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. */

Callers 1

mainMethod · 0.95

Calls 2

absMethod · 0.95
negateMethod · 0.95

Tested by

no test coverage detected