C++ solution to Project Euler Problem 1

Problem 1:

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.

Running time: Unknown

Assessment: First code I’d written in 7-8 years. I hadn’t started measuring execution time yet, so I’m not sure how long it took to run, but it’s basically instantaneous.

  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int main()
  5. {
  6. 	int total = 0;
  7. 	for (int i = 0; i < 1000; i++)
  8. 	{
  9. 		if( (i % 3 == 0) || (i % 5 == 0) )
  10. 			total += i;
  11. 	}
  12. 	cout << total;	
  13. 	return 0;
  14. }

2 thoughts on “C++ solution to Project Euler Problem 1

Leave a Reply

Your email address will not be published. Required fields are marked *