Overview

 

Question

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.

click for answer

233168

Solutions

 

C++

++ natural.cpp https://raw.github.com/addamh/euler/master/001/natural.cpp download


#include <stdio.h>
int main()
{
   int sum=0, i, three, five;
   for(i=0;i<1000;i++)
   {
      three=i%3;
      five=i%5;
      if(three==0 || five==0)
         sum+=i;
   }
   printf("%d\n", sum);
   return 0;
}
$ time bash natural.cpp
real	0m0.006s
user	0m0.002s
sys	0m0.002s

Ruby

 natural.rb https://raw.github.com/addamh/euler/master/001/natural.rb download
#!/usr/bin/env ruby
sum = 0
1000.times do |i|
	if i % 3 == 0 or i % 5 == 0
		sum += i
	end
end
puts sum
$ time ruby natural.rb
real	0m0.027s
user	0m0.022s
sys	0m0.004s