From 94248612ba6a45b26c84be89a3799f07253b35ea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=A1s=20A=2E=20Ortega?= Date: Mon, 6 Feb 2017 18:00:34 +0100 Subject: [PATCH] Find the nth prime. --- src/main.c | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/src/main.c b/src/main.c index 7d9002c..6a5f5c8 100644 --- a/src/main.c +++ b/src/main.c @@ -24,12 +24,13 @@ int main(int argc, char *argv[]) { f_version = false, f_quiet = false; int base = 10; + size_t n_prime = 0; char *dfile = NULL; char *efile = NULL; // Parse commandline arguments int c; - while((c = getopt(argc, argv, "hvqb:f:e:")) != -1) { + while((c = getopt(argc, argv, "hvqb:f:e:n:")) != -1) { switch(c) { case 'h': f_help = true; @@ -55,6 +56,14 @@ int main(int argc, char *argv[]) { case 'e': efile = optarg; break; + case 'n': + n_prime = atoi(optarg); + if(n_prime <= 2) { + fprintf(stderr, + "`n' must be larger than 2 (first two primes are 2 and 3).\n"); + return 1; + } + break; default: printUsage(argv[0]); return 1; @@ -70,7 +79,8 @@ int main(int argc, char *argv[]) { puts(" -q quiet mode"); puts(" -b base in which to print primes between 2 and 62 (default 10)"); puts(" -f file in/from which primes are stored and read from in raw format"); - puts(" -e export input file to plain text format\n"); + puts(" -e export input file to plain text format"); + puts(" -n run until the 'n'th prime\n"); return 0; } else if(f_version) { printf("Indivisible %s\n", VERSION); @@ -225,6 +235,8 @@ int main(int argc, char *argv[]) { printf("\n"); } } + + if(primes.end == n_prime) break; } @@ -260,7 +272,7 @@ releaseMemory: } void printUsage(char *progName) { - printf("%s [[-f [-e | -q]] [-b ] | [-h] | [-v]]\n", progName); + printf("%s [[-f [-e | -q]] [-b ] [-n ] | [-h] | [-v]]\n", progName); } void leave() { run = false; }