Sep 19, 2012 at 4:26am UTC
i'm trying to write a program that will do this:
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.
this is how my program looks like:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
#include <iostream>
double three, five;
int sumfive, sumthree, sum;
int main()
{
for (int a = 999; a = 0; a--)
{
three = a%3;
if (three = 0)
{
sumthree += a;
}
five = a%5;
if (five = 0)
{
sumfive += a;
}
}
sum = sumfive + sumthree;
std::cout << sum << std::endl;
system("pause" );
return 0;
}
i can't find any error but for some reason sum is always = 0
i can get it to work by chaning to a while-loop however it outputs the wrong number when using the same code but instead of a for loop i use
1 2 3
while (a < 1000)
(same code as above)
a++;
anyone got an idea? :<
Last edited on Sep 19, 2012 at 4:27am UTC
Sep 19, 2012 at 4:42am UTC
@Serri
You made a small error in your for loop.
Instead of..
for (int a = 999; a = 0; a--)
it should be..
for (int a = 999; a > 0; a--)
Sep 19, 2012 at 4:43am UTC
change line 13 to if (three == 0)
change line 23 to if (five == 0)
Also initialize sumfive and sumthree to 0. Even though your compiler may set it to zero, I don't think that this behavior is defined.
Sep 19, 2012 at 5:29am UTC
In this case, it is well defined. The variables must be set to 0 by a conforming compiler. Not initializing them explicitly, however, is a bad habit to get into and so is making all variables global.
Sep 19, 2012 at 6:33am UTC
thanks for the help guys it now looks like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
#include <iostream>
#include "windows.h"
double three, five;
int sumfive = 0, sumthree = 0;
int main()
{
for (int a = 999; a > 0; a--)
{
three = a%3;
if (three == 0)
{
sumthree += a;
}
five = a%5;
if (five == 0)
{
sumfive += a;
}
}
int sum = sumfive + sumthree;
std::cout << sum << std::endl;
system("pause" );
return 0;
}
however it outputs the wrong answer.
supposedly the sum of all the multiples of 3 or 5 below 1000 is 233168 my program outputs 266333.
Last edited on Sep 19, 2012 at 6:42am UTC
Sep 19, 2012 at 6:55am UTC
Try testing, with a smaller value like 50, use cout statements in the loops to print out intermediate values, so you can see what's going on.
Even better, learn how to use a debugger, - it's fairly easy with an IDE.
HTH
Sep 19, 2012 at 6:56am UTC
> supposedly the sum of all the multiples of 3 or 5 below 1000 is 233168 my program outputs 266333.
You are adding up multiples of 15 twice.
Let a be the sum of multiples of 3 below 1000
a == 3 + 6 + ..... + 999 == 3 * ( 1 + 2 + ... + 333 ) == 3 * 333 * 334 / 2
Let b be the sum of multiples of 5 below 1000
b == 5 + 10 + ..... + 995 == 5 * ( 1 + 2 + ... + 199 ) == 5 * 199 * 200 / 2
Let c be the sum of multiples of 15 below 1000
c == 15 + 30 + ..... + 990 == 15 * ( 1 + 2 + ... + 66 ) == 15 * 66 * 67 / 2
The sum of all the multiples of either 3 or 5 or both below 1000 == a + b - c
The sum of all the multiples of either 3 or 5 but not both below 1000 == a + b - c*2
Sep 19, 2012 at 5:45pm UTC
> only one of them should be added each time 3*x or 5*x becomes the same value
Yes.