Studiose Well I guess that's that.

Project Euler #1

Posted on May 25, 2010

"Add all the natural numbers below one thousand that are multiples of 3 or 5."

Started working on the Project Euler problems. Just going to work my way through all of them one at a time. I'm hoping it'll be an amazing learning experience.

The solution to problem #1 in C++:

int Problem1()
{
	for( int i = 1; i < 1000; i++ )
	{
		if( i % 3 == 0 || i % 5 == 0 ) Answer += i;
	}
	return Answer;
}

Simple enough, had myself a bit confused at first though making a silly mistake. Instead of checking if the value was divisible for both numbers I was checking if it was divisible for one and then checking if it was divisible for the other, meaning I was adding the multiple twice if it worked for both. But I realized my mistake and fixed it, slightly proud of myself.

Comments (0) Trackbacks (0)

No comments yet.


Leave a comment


No trackbacks yet.