-
Project Euler
Hi. I am sure y'all are familiar with Project Euler or may have finished it.
The first problem: find the sum of natural numbers between 0 and 1000 divisible by 3 or 5
I have attached my attempted solution.
Help. My codes not running for whatever reason. I need some pointers.
Thanks,'
CodeGuru25
-
You already have an iterator called "counter", working in a for loop, so ditch the while loop:
Code:
while((counter % 3)|| (counter % 5)= 0)
//make it
if ((counter % 3 == 0) || (counter % 5 == 0))
I'm not saying the while loop might not get the right answer - probably does. Conceptually, it's very wrong, however.
I've highlighted the other problem with the code. Also, don't "abbreviate" the mod operattion. It needs TWO == 0's.
-
Code:
for (counter = 0; counter < 1000; counter ++)
{
while((counter % 3)|| (counter % 5)= 0)
The inner loop is terribly wrong. It should be an if (counter divides by 3 or counter divides by 5) in the first place:
Code:
if (counter % 3 == 0 || counter % 5 == 0)
And the printf call is missing a comma between arguments.
There are other problems that might bite you at some time. For example, why is only the last of the three globals initialized to 0? (Luckily for you globals are automatically zero-initialized.)