Skip to content

Instantly share code, notes, and snippets.

@soravux
Last active December 15, 2015 23:09
Show Gist options
  • Save soravux/5337666 to your computer and use it in GitHub Desktop.
Save soravux/5337666 to your computer and use it in GitHub Desktop.
Solution to the Project Euler problem #1 - For the multigrad blog (entry 4)
#include <stdio.h>
unsigned int x = 0, state = 0;
unsigned int NumberIt()
{
/* Create a persistant variable x and initialize it to 0 */
switch (state) {
/* case 0: Replaced by default*/
case 3:
case 6:
x += 3;
break;
case 1:
case 5:
x += 2;
break;
case 2:
case 4:
x += 1;
break;
default:
state = 0;
x += 3;
}
state++;
return x;
}
int main()
{
unsigned int lResult, lTmpValue, lLoop;
for (lLoop = 0; lLoop <= 100000; lLoop++) {
lResult = 0;
while (1) {
lTmpValue = NumberIt();
if (lTmpValue >= 1000) {
break;
}
lResult += lTmpValue;
x = x - 1000;
state = 0;
}
}
printf("Result: %u\n", lResult);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment