/* * Based on Matt Segal (segal3@linkline.com) * Rob James (rbjames@home.com) * Chris Taylor (eagle19 at deja.com) * E.J. Wilburn (zane@supernova.org) * C++ code. * * Modified by Marek * * Compile: gcc -o primes primes.c -lm * or * gcc -o primes primes.c -DDOUBLE_OUTPUT -lm */ #include #include #include #include #define TRUE -1 #define FALSE 0 #define WHEEL_SIZE 48 #define WHEEL_MOD 210 const char *outfile_name = "found_primes.txt"; int wheel[WHEEL_SIZE] = { 1, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 121, 127, 131, 137, 139, 143, 149, 151, 157, 163, 167, 169, 173, 179, 181, 187, 191, 193, 197, 199, 209 }; int isprime(register long int); int isprime(register long int value_to_check) { register long int squareroot; register long int div; register long int mlt; register long int loop; if(value_to_check < 3){ if ((value_to_check == -1) || (value_to_check == 2)) return TRUE; else return FALSE; } if((value_to_check == 3) || (value_to_check == 5) || (value_to_check == 7)){ return TRUE; } if((value_to_check % 2 == 0) || (value_to_check % 3 == 0) || (value_to_check % 5 == 0) || (value_to_check % 7 == 0)){ return FALSE; } if(value_to_check < 50){ return TRUE; } squareroot = (long int) sqrt(value_to_check); div = 0; for(mlt = 0; div <= squareroot; mlt += WHEEL_MOD){ for(loop = (mlt > 0 ? 0: 1); loop < WHEEL_SIZE; loop++){ if((div = wheel[loop] + mlt) > squareroot) break; if(value_to_check % div == 0) return FALSE; } } return TRUE; } int main(int argc, char **argv) { register long int first_number; register long int last_number; register long int start; register int int_number; register long int long_number; long int counter = 0; FILE *outfile; if(argc!=2){ fprintf(stderr, "Usage: %s last_value_to_check\n", argv[0]); exit(1); } first_number = 1L; last_number = atol(argv[1]); if(last_number <= 0){ fprintf(stderr, "Error: bad parameter %s\n", argv[1]); exit(2); } outfile = fopen(outfile_name,"w"); if(!outfile){ fprintf(stderr, "Error: unable to open output file %s, giving up...\n", outfile_name); exit(3); } if(first_number < 4){ start = first_number; } else{ start = first_number % 6; switch (start){ case 0: case 6: start = first_number; break; default: start = ((first_number / 6) + (first_number < 0)? -1: 1) * 6; } } for(int_number = start; int_number < 4 && int_number < last_number; int_number++){ if (isprime(int_number)){ counter++; #ifdef DOUBLE_OUTPUT printf("%li\n", int_number); #endif fprintf(outfile, "%li\n", int_number); } } if(start < 6){ start = 6; } for(long_number = start; long_number - 1 <= last_number ; long_number += 6){ if ((long_number - 1 >= first_number) && (long_number - 1 <= last_number) && isprime(long_number - 1)){ counter++; #ifdef DOUBLE_OUTPUT printf("%li\n", long_number - 1); #endif fprintf(outfile,"%li\n", long_number - 1); } if ((long_number + 1 >= first_number) && (long_number + 1 <= last_number) && isprime(long_number + 1)){ counter++; #ifdef DOUBLE_OUTPUT printf("%li\n", long_number + 1); #endif fprintf(outfile,"%li\n", long_number + 1); } } fclose(outfile); printf("\n%i primes between 1 and %li written into output file %s\n\n", counter, last_number, outfile_name); return 0; }