Created
October 22, 2012 07:56
-
-
Save holderbaum/3930213 to your computer and use it in GitHub Desktop.
generate primes - non-optimized naive algorithm!
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
char is_prime(int number) { | |
int i; | |
int limit = number/2; | |
for(i=2;i<=limit;i++) { if(number % i == 0) return 0; } | |
return 1; | |
}; | |
int main(int argc, char* argv[]) { | |
if(argc != 4) { | |
printf("usage: %s start max_number filename\n", argv[0]); | |
exit(1); | |
} | |
int start = atoi(argv[1]); | |
int max = atoi(argv[2]); | |
FILE *result = fopen(argv[3], "w"); | |
int i; | |
int hits = 0; | |
for(i=start;max==0||hits<max;i++) { | |
if(is_prime(i)) { | |
fprintf(result, "%d\n", i); | |
fflush(result); | |
hits++; | |
} | |
} | |
fclose(result); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment