Problem
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.
- #include <stdio.h>
-
-
-
#define NUM 999
-
-
int main(int argc, const char *argv[])
-
{
-
int i, sum = 0;
-
#if 0
-
-
for (i = 0; i <= NUM; i++) {
-
if (!(i%3 && i%5)) {
-
sum += i;
-
}
-
-
}
-
printf("sum = %d\n", sum);
-
-
#else
-
sum = 0;
-
sum += (3+(NUM - (NUM%3)))* (NUM/3)/2 ;
-
sum += (5+(NUM - (NUM%5)))* (NUM/5)/2 ;
-
sum -= (15+(NUM - (NUM%15)))* (NUM/15)/2 ;
-
printf("sum = %d\n", sum);
-
#endif
-
-
-
return 0;
-
}
阅读(1518) | 评论(0) | 转发(0) |