feat: add prime
This commit is contained in:
parent
9c5776b620
commit
68d3c97905
|
@ -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 <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -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 *~
|
|
@ -0,0 +1,4 @@
|
||||||
|
# go: recipe with dependencies
|
||||||
|
go: go.c primes.c
|
||||||
|
gcc -o go go.c 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;
|
||||||
|
}
|
|
@ -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 */
|
|
@ -0,0 +1 @@
|
||||||
|
[source](https://gribblelab.org/teaching/CBootCamp/12_Compiling_linking_Makefile_header_files.html)
|
|
@ -13,6 +13,7 @@ int main()
|
||||||
char * secret = "secret";
|
char * secret = "secret";
|
||||||
char * host = "localhost";
|
char * host = "localhost";
|
||||||
char * db = "vbn_data";
|
char * db = "vbn_data";
|
||||||
|
/*TODO How is const defined?*/
|
||||||
const int pqConInfoSize = 100;
|
const int pqConInfoSize = 100;
|
||||||
char pqConInfo[pqConInfoSize];
|
char pqConInfo[pqConInfoSize];
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue