Introduction
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 N.
Input and Output Format
|
|
{: file="Input and Output” }
Explanation
For N = 10, 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.
Similarly for N = 100, we get 2318.
Process
Using a for loop to loop through all the numbers below N, and check if the number is a multiple of 3 or 5, if it is, add it to the sum, and print out the sum at the end of the loop.
Code
|
|
{: file="Multiples of 3 and 5.c” }
Afterthoughts
This solution was a bit of a brute force solution, but it worked. It passed 4/6
test cases and needed to be optimized. I’m sure there’s a more efficient way to do this, but I’ll take a second look at this later.