From a783d4de11cfb436264f50eb9a16a3920ba88bf2 Mon Sep 17 00:00:00 2001
From: Porter Libby <pelibby16@earlham.edu>
Date: Mon, 26 Feb 2024 17:04:40 -0500
Subject: [PATCH] add primes.c

---
 cs-fundamentals/batch-computing.md  |  2 +-
 cs-fundamentals/c-examples/primes.c | 59 +++++++++++++++++++++++++++++
 2 files changed, 60 insertions(+), 1 deletion(-)
 create mode 100644 cs-fundamentals/c-examples/primes.c

diff --git a/cs-fundamentals/batch-computing.md b/cs-fundamentals/batch-computing.md
index e2e78a9..33ea4de 100644
--- a/cs-fundamentals/batch-computing.md
+++ b/cs-fundamentals/batch-computing.md
@@ -17,7 +17,7 @@ You can SSH to these machines by first connecting to Hopper, and then to either
 
 ## Deliverables
 
-Use the [area under a curve C program](c-examples/auc-trapazoidal-serial.c) provided in this module as the basis for your own program. Compile and run it interactively varying the input until you understand how it scales based on the different parameters. 
+Use [auc-trapazoidal-serial.c](c-examples/auc-trapazoidal-serial.c) or [primes.c](c-examples/primes.c) provided in this module as the basis for your own program. Compile and run it interactively varying the input until you understand how it scales based on the different parameters. 
 
 Experiment with differnet choices for a, b and n until the job takes about an hour to run on a machine like Hamilton or Whedon. You can time the job by putting `time` in front of your other commands on the command line.
 
diff --git a/cs-fundamentals/c-examples/primes.c b/cs-fundamentals/c-examples/primes.c
new file mode 100644
index 0000000..82d8fc9
--- /dev/null
+++ b/cs-fundamentals/c-examples/primes.c
@@ -0,0 +1,59 @@
+/* Primes.c */
+/* get prime numbers between int a and int b */
+/* ./primes -a 1 -b 100 */
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <math.h>
+#include <getopt.h>
+int main(int argc, char *argv[]) {
+    unsigned long long a = 1;
+    unsigned long long a2 = a;
+    unsigned long long b = 100;
+    int debug = 0, opt = 0;
+    int c = 0;
+    int count, i;
+    // get some args
+    while ((opt = getopt(argc, argv, "da:b:n:")) != -1) {
+        switch (opt) {
+            case 'd':
+                debug = 1;
+                break;
+
+            case 'a':
+                a = strtoul(optarg, (char**) NULL, 10);
+		a2 = a;
+                break;
+
+            case 'b':
+                b = strtoul(optarg, (char**) NULL, 10);
+                break;
+
+            case '?':
+            case 'h':
+            case 'H':
+                fprintf(stderr, "usage: -a <range-start> -b <range-end> -n\n");
+                fprintf(stderr, "without any arguments runs with a=1, b=100\n");
+                exit(1);
+            default:
+                break;
+        }
+    }
+    while(a <= b){
+        count = 0;
+        i = 2;
+        while(i <= a/2){
+            if(a%i == 0){
+                count++;
+                break;
+            }
+            i++;
+        }
+        if(count == 0 && a != 1 ){
+            c++;
+        }
+        a++;
+    }
+    fprintf(stdout, "a = %d, b = %d, Total Primes = %d\n", a2, b, c);
+    return 0;
+}
\ No newline at end of file
-- 
GitLab