Skip to main content
Code Review

Return to Answer

replaced http://codereview.stackexchange.com/ with https://codereview.stackexchange.com/
Source Link

Thanks for @MartinR @MartinR for the hint, and @BorisTheSpider @BorisTheSpider to point out the problem with my original answer that didn't subtract.

Thanks for @MartinR for the hint, and @BorisTheSpider to point out the problem with my original answer that didn't subtract.

Thanks for @MartinR for the hint, and @BorisTheSpider to point out the problem with my original answer that didn't subtract.

added 380 characters in body
Source Link
janos
  • 112.9k
  • 15
  • 154
  • 396

Use theThe Math, @Heslacher ...

... well ok, in the case of one or two arguments, there's a simple math solution. Instead of iterating over the numbers in the range and performing modulo, you can you could:

The code becomesWith a helper function:

int sum =private 0;
foreachint SumOfMultiples(int multipletarget, inint multiplesmultiple) {
 int count = (target - 1) / multiple;
 sum +=return multiple * count * (count + 1) / 2;
}
return sum;

... unfortunately ...

As @BorisTheSpider pointed out in This gives a comment, andstraight \$O(1)\$ solution for one multiple. @Edward demonstrated in his answerFor two multiples a and b, this algorithm results in double-countingyou cannot simply sum them as that would include the common multiples. So in case of 3 and 5their multiples twice. So after summing, you would have to subtractsubtract the multiples of 3 * 5a * b:

return SumOfMultiples(target, a)
 + SumOfMultiples(target, b)
 - SumOfMultiples(target, a * b);

Thanks for @MartinR for the hint, and @BorisTheSpider to point out the problem with my original answer that didn't subtract.

Use the Math, @Heslacher ...

Instead of iterating over the numbers in the range and performing modulo, you can:

The code becomes:

int sum = 0;
foreach (int multiple in multiples) {
 int count = (target - 1) / multiple;
 sum += multiple * count * (count + 1) / 2;
}
return sum;

... unfortunately ...

As @BorisTheSpider pointed out in a comment, and @Edward demonstrated in his answer, this algorithm results in double-counting the common multiples. So in case of 3 and 5, you would have to subtract multiples of 3 * 5.

Use The Math, @Heslacher ...

... well ok, in the case of one or two arguments, there's a simple math solution. Instead of iterating over the numbers in the range and performing modulo, you could:

With a helper function:

private int SumOfMultiples(int target, int multiple) {
 int count = (target - 1) / multiple;
 return multiple * count * (count + 1) / 2;
}

This gives a straight \$O(1)\$ solution for one multiple. For two multiples a and b, you cannot simply sum them as that would include the multiples of their multiples twice. So after summing, you would have to subtract the multiples of a * b:

return SumOfMultiples(target, a)
 + SumOfMultiples(target, b)
 - SumOfMultiples(target, a * b);

Thanks for @MartinR for the hint, and @BorisTheSpider to point out the problem with my original answer that didn't subtract.

added 803 characters in body
Source Link
janos
  • 112.9k
  • 15
  • 154
  • 396

... unfortunately ...

As @BorisTheSpider pointed out in a comment, and @Edward demonstrated in his answer, this algorithm results in double-counting the common multiples. So in case of 3 and 5, you would have to subtract multiples of 3 * 5.

It's easy to handle two parameters like 3 and 5 in this example. But handling arbitrary number of parameters, the math gets a lot trickier.

In conclusion, when multiples.Length <= 2, a fairly simple math solution exists. When multiples.Length > 2 the implementation needs more work, and more math.

Your solution with iteration may be a bit inefficient, but it's simple and it works.

... unfortunately ...

As @BorisTheSpider pointed out in a comment, and @Edward demonstrated in his answer, this algorithm results in double-counting the common multiples. So in case of 3 and 5, you would have to subtract multiples of 3 * 5.

It's easy to handle two parameters like 3 and 5 in this example. But handling arbitrary number of parameters, the math gets a lot trickier.

In conclusion, when multiples.Length <= 2, a fairly simple math solution exists. When multiples.Length > 2 the implementation needs more work, and more math.

Your solution with iteration may be a bit inefficient, but it's simple and it works.

fixed foreach loop to be C# instead of java
Source Link
Heslacher
  • 50.9k
  • 5
  • 83
  • 177
Loading
added 1099 characters in body
Source Link
janos
  • 112.9k
  • 15
  • 154
  • 396
Loading
Source Link
janos
  • 112.9k
  • 15
  • 154
  • 396
Loading
lang-cs

AltStyle によって変換されたページ (->オリジナル) /