Originally Posted by
Boredpayne
You're not trying to find the number of multiples, you're trying to find the sum of all multiples.
Anyways, what I believe to be the optimal solution in java:
Finds all multiples of 5 that are not also multiples of 3, and then also finds all multiples of 3. This will find the correct solution. Quite an easy problem.
Oh I just looked at what Ryan wrote and achieved the same thing he did lol
Originally Posted by
Boredpayne
Java code:
int finalSum=0;
for(int i=0;i<1000;i=i+5) {finalSum=(i%3==0)?finalSum:finalSum+i;}
for(int j=0;j<1000;j=j+3) {finalSum=finalSum+j;}
return finalSum;
May as well correct some things. For starters you are using a weird ass syntax for the loops, just use
for(int i=0;i<1000;i=i+5) finalSum=(i%3==0)?finalSum:finalSum+i;
I forgot about java shorthand, but it is kind of strange to assign a variable to it'self. I am not sure if the Java compiler is smart enough to correct that...
Even though it looks messier,
for(int i=0;i<1000;i=i+5) if (i%3!=0) finalSum+i;
is definitely cleaner.
Also remember to use A++ rather than A = A + 1, and A+=5 rather than A = A + 5 that is considerably more lifting.
Comparing
int sum=0;
for(int i=0;i<1000;i+=5) if (i%3!=0) sum+=i;
for(int i=0;i<1000;i+=3) sum+=i;
return sum;
and
int sum=0;
for(int i=0;i<1000;i+=1) if (i%3==0 || i%5==0) sum+=i;
return sum;
Is a little tricky. Obviously we have a 200 loop and a 300 loop vs a 1000 loop, but also remember that the 2 loop solution has more code, and more variable reads/writes.
Obliviously it is more elegant to step by an amount that you know should be more successful. But in this case it will make it less scalable; for example if you need to check for 3, then you would need 3 loops, etc.
I would suggesting using
int t=0;
for(int i=0;i<1000;i++) if(i%3==0||i%5==0) t+=i;
return t;
I would say this is both the simplest and most elegant solution.
Originally Posted by
Pedant
C++ code:
int main()
{ int total = 0;
for (int i=0; i<1000; i++)
{
if ((i % 3 == 0) || (i % 5 == 0)) total += i;
}
printf("%d", total);
}
Not to nitpick, but your formatting is pretty bad @_@
"int total = 0;" should be on a new line, and the { should be on the previous line. Same thing with the for loop. This is called "one true brace" style, and it should definitely be used
Also use 4 spaces = 1 tab, that is the standard.
Also, you use inline if statement but you nest your for loop? I don't understand why such a radical shift!
Remember you can just say
for (int i=0; i<1000; i++) if ((i % 3 == 0) || (i % 5 == 0)) total += i;
Or
for (int i=0; i<1000; i++)
____if ((i % 3 == 0) || (i % 5 == 0)) total += i;
Also, (i%3== 0 || i%5== 0) is not ambiguous.
It is too bad that c++ doesn't support N-logic
It is also too bad that c defines true = !0...
This could be abused to produce
!(i%3&&i%5)
Which saves... 2 characters ._. if i counted correctly.
So in total that moves it to
int t=0;
for(int i=0;i<1000;i++)if(!(i%3&&i%5))t+=i;
return t;
Which is considerably better, but I still feel like it could be improved.
Last edited by Gorman; May 24, 2011 at 11:30 AM.