Originally Posted by
MacGyver
How about writing an entire program out as requested?
Then after that, check the flags your IDE has set for when it compiles and links.
This is the code in question. I have however narrowed it to a problem with notepad, using the > operator in cmd to redirect stdout to primes.txt gives the same results.
I also removed optimization from the compiling process - No change.
Implementation of Sieve of Eratosthenes.
Code:
#include <stdio.h>
#include <math.h>
int main(int argc, char **argv)
{
unsigned int limit = 100000000, *digits = malloc(sizeof(unsigned int)*limit), idx, idx1 = 1, max = sqrt(limit), store = 0;
fprintf( stderr, "Generating primes.. ");
for(idx = 0; idx < limit; idx++)
digits[idx] = idx1+=2;
for(idx = 0; idx < max; idx++)
{
if(digits[idx] == 0) continue;
store = digits[idx];
for(idx1 = idx; idx1 < limit; idx1 += store)
{
if(digits[idx1] == store) continue;
digits[idx1] = 0;
}
}
fprintf( stderr, "Done!\n");
fprintf( stderr, "Writing primes to file.. ");
FILE *out = fopen("primes.txt", "w+");
for(idx = 0; idx < 1000; idx++) {
if(digits[idx] != 0) { fprintf( stdout, "%d ", digits[idx]); }
}
fclose(out);
fprintf( stderr, "Done!\n");
free(digits);
}