## Thursday, October 22, 2009

### How to add all the natural numbers below one thousand that are multiples of 3 or 5

If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.

Find the sum of all the multiples of 3 or 5 below 1000.
How:

1. First we determine how many multiples are of 3, 5 and 15
2. Multiples of 3 are 333 => Take Quotient of (1000/3) which is 333
3. Similarly Multiples of 5 below 1000 are 199 => Take Quotient of (1000/5) which is 199
4. We need to sum up the multiples of 3 or 5 but there exist some numbers which are multiples of 3 as well as of 5 so we need to subtract that common multiples
5. We determined that multiples of 15 are that numbers which are multiples of 3 as well as of 5 and multiples of 15 below 1000 are 66 => Take quotient of (1000/15) which is 66

public void SumUpMultiples()
{
int sum = 0;
// From 1 to 66, we sum up the multiples of 3,5 but subtract multiples of 15
for (int i = 1; i <= 66; i++)
{
sum += ((i * 3) + (i * 5)) - (i * 15);
}
// From 67 to 199, we sum up the multiples of 3,5 only
for (int j = 67; j <= 199; j++)
{
sum += (j * 3) + (j * 5);
}
// From 200 to 333, we sum up the multiples of 3 only
for (int k = 200; k <= 333; k++)
{
sum += (k * 3);
}
}

Shahid Riaz Bhatti

Sheikh sab BS dobara se to start nahe kar dia?