Skip to content

Commit

Permalink
Merge pull request pedrovgs#5 from IsaacCisneros/p20-patch
Browse files Browse the repository at this point in the history
Remove the '*' operator completely from the algorithm
  • Loading branch information
pedrovgs committed May 17, 2015
2 parents 1f8f61c + 22ee3ce commit 128ccf0
Showing 1 changed file with 6 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,16 @@ public class MultiplicationWithoutMultiply {
*/
public int calculate(int n1, int n2) {
int result = 0;
boolean negative = n1 < 0;
boolean negative = (n1 < 0 && n2 >= 0) || (n2 < 0 && n1 >= 0);
boolean positive = !negative;
n1 = Math.abs(n1);
for (int i = 0; i < n1; i++) {
result += n2;
if (negative && n2 > 0 || positive && n2 < 0)
result -= n2;
else
result += n2;
}

if (negative) {
result *= -1;
}
return result;
}
}

0 comments on commit 128ccf0

Please sign in to comment.