Toribash
Original Post
Challenge
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 project euler challenge 1. I'd like to see how many languages we can write a solution in.
I'll write a few and post them here soon. This is for fun, so make your code as short or as efficient as you can.

PHP Code:
function all_multiples($num) {
  
$arr = array();
  for (
$i $num$i 1000$i += $num)
    
$arr[] = $i;
  return 
$arr;
}

$result array_map('all_multiples', array(35));
$end_result array_unique(array_merge($result[0], $result[1]));
sort($end_result);

echo(
array_sum($end_result)); 
Haskell code:
sum [x | x <- [1 .. 999], x `mod` 3 == 0 || x `mod` 5 == 0]


Ruby code:
total = 0
(1 .. 999).each { |i|
if (i % 3 == 0 or i % 5 == 0) then
total += i
end
}
Last edited by Ryan_old2; May 23, 2011 at 03:07 PM.
C# code:
int total = 0;
for(int i=1;i<1000;i++) total+=(i%3==0||i%5==0)?1:0;
return total;

C++ code:
int total = 0;
for(int i=1;i<1000;i++) if(i%3==0||i%5==0) total++;
return total;

C code:
int total = 0;
for(int i=1;i<1000;i++) if(i%3==0||i%5==0) total++;
return total;

Java code:
int total = 0;
for(int i=1;i<1000;i++) if(i%3==0||i%5==0) total++;
return total;

VB code:
Dim total = 0
For i = 1 To 999 Step 1
If ((i mod 3)=0 Or (i mod 5)=0) Then
total = total+1
End IF
Next i
Return total

F# code:
let mutable total = 0
let pool={1..1000}
let filter =
pool
|> Seq.filter (fun x -> i%3 = 0 || i%5 = 0)
|> Seq.map (fun x -> total <- total + 1)
return total

didn't test any of them lol \o/


You know, it would probably be more efficient to do something like;
(int)999/3 + (int)999/5 - (int)999/15
rather than looping (didn't check my maths, but it seems legit to me)
rather than 999/3 (333) + 999/5 (199) - 999/15 (66), (= 466) you'd use

(int) 3(333 * (333+1) / 2) + (int) 5(199 * (199+1) / 2) - (int) 15(66 * (66+1) / 2)
Ruby code:
number=0
for i in 1..999
if i%3==0 or i%5==0
number=number+i
end
end
puts number.to_s


My version of the ruby solution.
I'll do lua later.
multiple texture uploader! updated: multiple texture remover!
updated pretty colorlist!

<BobJoelZ> ok ive just rebooted my pc and ive tried to activate my reflex on yahoo internet explorer :/ no luck

<Aracoon> I do not enjoy having anal sex with multiple men
javascript code:
var total = 0;

for(var i = 0; i < 1000; i++) {
if(i % 3 == 0 || i % 5 == 0) {
total += i;
}
}

alert(total);


Also, the following function ported to any language and called with parameters 3 and 5:
function f(n1, n2) {
  return n1*((1000/n1) * ((1000/n1)+1) / 2) + n2*((1000/n2) * ((1000/n2)+1) / 2) - (n1*n2)*((1000/(n1*n2) * ((1000/(n1*n2))+1) / 2);
}
Last edited by Ryan_old2; May 23, 2011 at 03:59 PM.
Originally Posted by Gorman View Post
Java code:
int total = 0;
for(int i=1;i<1000;i++) if(i%3==0||i%5==0) total++;
return total;

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:

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;


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.
Buy TC for a great price here! http://forum.toribash.com/showthread.php?t=240345
Buy VIP and Toriprime for a great price here! http://forum.toribash.com/showthread.php?t=237249


Hey look more than two lines.
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);
}
Originally Posted by Boredpayne View Post
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 View Post
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 View Post
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.
I'd usually write it very differently (below), but I was in the mood for making stuff look strange and following a convention somebody I know uses. I don't really like it. Using my normal style:
C++ code:
int total = 0;
for (int i=0; i<1000; i++)
if (i % 3 == 0 || i % 5 == 0)
total += i;
return total;


I like my two space indents. :D
-----
Oh, and I'm learning J. This works:
J code:
(+/(0=(3|/i.1000))#i.1000)+(+/(0=(5|/i.1000))#i.1000)-(+/(0=(15|/i.1000))#i.1000)

Not the most elegant or the most efficient code, but it works.
Last edited by Ryan_old2; May 24, 2011 at 05:58 PM. Reason: <24 hour edit/bump
Lua code:
print(3*((1000/3) * ((1000/3)+1) / 2) + 5*((1000/5) * ((1000/5)+1) / 2) - 15*((1000/15 * ((1000/15)+1) / 2)))