In my seventh year, the new millennium.
(colour)
(black-and-white)
物理子衿
In my seventh year, the new millennium.
(colour)
(black-and-white)
If we list all the natural numbers below that are multiples of
or
, we get
,
,
and
. The sum of these multiples is
.
1 2 3 4 5 6 7 8 9 |
/* running a check on the first two statements by a direct computation */ #include <stdio.h>; #include <stdlib.h>; int main() { printf("The sum of all the natural numbers below 10 that are multiples of 3 and 5 is %d.", 3 + 5 + 6 + 9); system("pause"); return 0; } |
Find the sum of all the multiples of or
below
.
Ans.
(step-by-step approach, slow but sure logic)
I wish to find all the multiples of (excluding multiples of
) below
:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
/* finding all the multiples of 3 (excluding multiples of 5) below 1000*/ #include <stdio.h>; #include <stdlib.h>; int main() { int sum = 0; for (int i = 0; i < 1000; i += 3) { if (i % 5 != 0) { sum += i; } } printf("The sum of multiples of three (excluding multiples of five) below one thousand is %d.", sum); } |
1 2 |
Output. The sum of multiples of three (excluding multiples of five) below one thousand is 133668. |
Then I wish to find all the multiples of (excluding multiples of
) below
:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
/* finding all the multiples of 5 (excluding multiples of 3) below 1000:*/ #include <stdio.h>; #include <stdlib.h>; int main() { int sum = 0; for (int i = 0; i < 1000; i += 5) { if (i % 3 != 0) { sum += i; } } printf("The sum of multiples of five (excluding multiples of three) below one thousand is %d.", sum); } |
1 2 |
Output. The sum of multiples of five (excluding multiples of three) below one thousand is 66335. |
Lastly I wish to find all the multiples of below
:
1 2 3 4 5 6 7 8 9 10 11 12 |
/* finding all the multiples of 15 below 1000:*/ #include <stdio.h>; #include <stdlib.h>; int main() { int sum = 0; for (int i = 0; i < 1000; i += 15) { sum += i; } printf("The sum of multiples of fifteen below one thousand is %d.", sum); } |
1 2 |
Output. The sum of multiples of fifteen below one thousand is 33165. |
In sum,
Ans.
(to be continued)
Going off at a tangent, the sum of all numbers from to
is
. See:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
#include <stdio.h>; #include <stdlib.h>; int main() { int i,sum; i = 1; sum = 0; do { sum += i++; } while (i <= 1000); printf("sum = %d", sum); system("pause"); return 0; } |
1 2 |
Output. sum = 500500 |
(proof)
(continue)
Solution. (fast and furious attempt)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
/* The sum of all the multiples of 3 or 5 below 1000 */ #include <stdio.h>; #include <stdlib.h>; int main() { int sum = 0; int i = 0; while(i < 1000) { if(i % 3 == 0 || i % 5 == 0) sum += i; i++; } printf("The sum of all the multiples of 3 or 5 below 1000 is %d.", sum); } |
1 2 |
Output. The sum of all the multiples of 3 or 5 below 1000 is 233168. |
Given two operands and
, let them be integers:
1 2 3 4 |
int a; int b; printf("Please input two integers \n==>"); scanf("%d %d", &a, &b); |
Addition Operator adds two operands
,
:
1 2 |
sum = a + b; printf("The sum is %d\n", sum); |
Subtraction Operator subtracts the second operand
from the first operand
:
1 2 |
difference = a - b; printf("The difference is %d\n", difference); |
Multiplication Operator multiplies both operands
,
:
1 2 |
product = a * b; printf("The product is %d\n", product); |
Division Operator divides the first operand
(numerator) by the second operand
(denominator):
1 2 |
quotient = a / b; printf("The quotient is %d\n", quotient); |
Modulo Operator returns the remainder after integer division of the first operand
(dividend) by the second operand
(divisor):
1 2 |
s = a % b; printf("a mod b is %d\n", s); |
Increment Operator increases the integer value by one.
1 2 3 4 5 6 |
int a; int b; //pre-increment b = ++a; //post-increment b = a++; |
Decrement Operator decreases the integer value by one.
1 2 3 4 5 6 |
int a; int b; //pre-decrement b = --a; //post-decrement b = a--; |
Cf.
The pre-increment and pre-decrement operators increment (decrement resp.) their operand by 1, and the value of the expression is the resulting incremented (decremented resp.) value. The post-increment and post-decrement operators increase (decrease resp.) the value of their operand by 1, but the value of the expression is the operand’s value prior to the increment (decrement resp.) operation.
Wikipedia on Increment and decrement operators