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.
What Does It Mean?
We need to add up all the numbers between 1 and 1000 (but not 1000) which are an multiple of 3 or 5. The easiest way would to have a loop with a two step test for the multiple. Pass a test, get added to the sum. If we can pull off both tests in one line, that should speed things up. Maybe use a Mod3 and Mod5 if it’s available for the language? Well, let’s get some pseudocode
We should probably define some variables and some includes, depending on the language.
include needed stuff current_num sum loop start at 1 go to less than 1000 check if current_num is multiple of 3? If true, add to sum If false, is current_num a multiple of 5? If true, add to sum If false, test next number print sum exit program
Should I have it print each step of the tests? Might be a good idea for the test versions? Maybe add a –quiet/q flag and the option to set the max value to test up to?
I rewrote the code a bit, here it is:
include needed stuff # Define vars early if we need to. Use them in first time in blocks if allowed current_num sum loop start at 1 go to less than 1000 check if current_num is multiple of 3 or 5 print message about currect_num add current_num to sum end loop print sum