From 68d3c979050be6e8e1cbfd0eb32c0ab794016540 Mon Sep 17 00:00:00 2001 From: "Begerad, Stefan" Date: Thu, 19 Jan 2023 08:30:50 +0100 Subject: [PATCH] feat: add prime --- prime/go.c | 24 ++++++++++++++++++++++++ prime/makefile | 14 ++++++++++++++ prime/makefile1 | 4 ++++ prime/primes.c | 16 ++++++++++++++++ prime/primes.h | 7 +++++++ prime/readme.md | 1 + sprintf/main.c | 1 + 7 files changed, 67 insertions(+) create mode 100644 prime/go.c create mode 100644 prime/makefile create mode 100644 prime/makefile1 create mode 100644 prime/primes.c create mode 100644 prime/primes.h create mode 100644 prime/readme.md diff --git a/prime/go.c b/prime/go.c new file mode 100644 index 0000000..fd2d4f6 --- /dev/null +++ b/prime/go.c @@ -0,0 +1,24 @@ +/* go.c + Takes one input argument from the command line, an integer, + and returns 1 if the number is prime, and 0 if it is not. + + Compile with: gcc -o go go.c primes.c +*/ + +#include +#include +#include "primes.h" + +int main(int argc, char *argv[]) { + if (argc < 2) { + printf("error: must provide a single integer value to test\n"); + return 1; + } + else { + int n = atoi(argv[1]); + int prime = isPrime(n); + printf("isPrime(%d) = %d\n", n, prime); + return 0; + } +} + diff --git a/prime/makefile b/prime/makefile new file mode 100644 index 0000000..8a61fa6 --- /dev/null +++ b/prime/makefile @@ -0,0 +1,14 @@ +RM = /bin/rm -f +CC = gcc +CFLAGS = -Wall +DEPS = primes.h +OBJ = go.o primes.o +#%.o recipe with dependencies +%.o: %.c $(DEPS) + $(CC) $(CFLAGS) -c -o $@ $< +#go recipe with dependencies +go: $(OBJ) + gcc $(CFLAGS) -o $@ $^ +# +clean: + $(RM) $(OBJ) go *~ diff --git a/prime/makefile1 b/prime/makefile1 new file mode 100644 index 0000000..c8586e2 --- /dev/null +++ b/prime/makefile1 @@ -0,0 +1,4 @@ +# go: recipe with dependencies +go: go.c primes.c + gcc -o go go.c primes.c + diff --git a/prime/primes.c b/prime/primes.c new file mode 100644 index 0000000..8c381bb --- /dev/null +++ b/prime/primes.c @@ -0,0 +1,16 @@ +int isPrime(int n) { + + // returns 0 if not prime, 1 if prime + + if (n<2) return 0; // first prime number is 2 + if (n==2) return 1; // ensure 2 is identified as a prime + if ((n % 2)==0) return 0; // all even numbers above 2 are not prime + + int i; + for (i=3; i*i < n; i++) { // test divisibility up to sqrt(n) + if ((n % i) == 0) { + return 0; + } + } + return 1; +} diff --git a/prime/primes.h b/prime/primes.h new file mode 100644 index 0000000..b191ca5 --- /dev/null +++ b/prime/primes.h @@ -0,0 +1,7 @@ +/*We can use CPP tricks to avoid parsing the same header file more than once*/ +#ifndef PQ_CON_INFO_H +# define PQ_CON_INFO_H + +int isPrime(int n); // returns 0 if n is not prime, 1 if n is prime + +#endif /* PQ_CON_INFO_H */ diff --git a/prime/readme.md b/prime/readme.md new file mode 100644 index 0000000..f432aba --- /dev/null +++ b/prime/readme.md @@ -0,0 +1 @@ +[source](https://gribblelab.org/teaching/CBootCamp/12_Compiling_linking_Makefile_header_files.html) diff --git a/sprintf/main.c b/sprintf/main.c index 0201af5..3f95efb 100644 --- a/sprintf/main.c +++ b/sprintf/main.c @@ -13,6 +13,7 @@ int main() char * secret = "secret"; char * host = "localhost"; char * db = "vbn_data"; + /*TODO How is const defined?*/ const int pqConInfoSize = 100; char pqConInfo[pqConInfoSize];