202108050932 Project Euler Problem 1 solved by C

Problem 1 of Project Euler

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.

Ans. 233168


(step-by-step approach, slow but sure logic)

I wish to find all the multiples of 3 (excluding multiples of 5) below 1000:

Then I wish to find all the multiples of 5 (excluding multiples of 3) below 1000:

Lastly I wish to find all the multiples of 15=3\times 5 below 1000:

In sum,

\begin{aligned} & \quad \textrm{The sum of all multiples of 3 or 5 below 1000} \\ & = 133\small,668 + 66\small,335 + 33\small,165 \\ & = 233\small,168 \end{aligned}

Ans. 233168 \quad\checkmark

(to be continued)


Going off at a tangent, the sum of all numbers from 1 to 1000 is 500500. See:

(proof)

\begin{aligned} & \quad 1+2+3+\cdots +1000 \\ & = \sum_{k=1}^{1000}k \\ & = \frac{(1000)(1000+1)}{2} \\ & = 500500 \\ \end{aligned}


(continue)

Solution. (fast and furious attempt)

202108041142 Arithmetic Operators in C

Given two operands a and b, let them be integers:

Addition Operator (+) adds two operands a, b:

Subtraction Operator (-) subtracts the second operand b from the first operand a:

Multiplication Operator (*) multiplies both operands a, b:

Division Operator (/) divides the first operand a (numerator) by the second operand b (denominator):

Modulo Operator (\% ) returns the remainder after integer division of the first operand a (dividend) by the second operand b (divisor):

Increment Operator (++) increases the integer value by one.

Decrement Operator (--) decreases the integer value by one.


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