I recently started learning C. I'm using Code :: Blocks with MinGW and Cygwin GCC.
I made a very simple right sieve for the Project Euler 10 task, which prints primes below a certain limit for stdout. It works fine up to about 500,000 as a limit, but is higher than my compiled with minGW.exe failure, and the compiled GCC code throws an exception "STATUS_STACK_OVERFLOW".
I am puzzled by why, because the code is completely non-recursive, consisting of simple loops.
#include <stdio.h> #include <math.h> #define LIMIT 550000 int main() { int sieve[LIMIT+1] = {0}; int i, n; for (i = 2; i <= (int)floor(sqrt(LIMIT)); i++){ if (!sieve[i]){ printf("%d\n", i); for (n = 2; n <= LIMIT/i; n++){ sieve[n*i] = 1; } } } for (i; i <= LIMIT; i++){ if (!sieve[i]){ printf("%d\n", i); } } return 0; }
source share