From 72811cbcd3b75468813fc79ad6054cada94914f5 Mon Sep 17 00:00:00 2001 From: Jonathan Chan Date: Sun, 26 Aug 2018 23:38:09 -0700 Subject: [PATCH] Style changes to primes --- primes.c | 20 ++++++++++---------- primes.h | 10 ++++++---- primes.o | Bin 0 -> 2976 bytes 3 files changed, 16 insertions(+), 14 deletions(-) create mode 100644 primes.o diff --git a/primes.c b/primes.c index 7ee57b0..aa5a5a9 100644 --- a/primes.c +++ b/primes.c @@ -8,7 +8,7 @@ * runs in O(sqrt(n)) * */ -bool isPrime (long n) { +bool isPrime(long n) { if (n == 0 || n == 1) return false; @@ -26,7 +26,7 @@ bool isPrime (long n) { * runs in O(sqrt(n)/ln(n)) * */ -bool isPrimeMem (long n, long* primes) { +bool isPrimeMem(long n, long* primes) { if (n == 0 || n == 1) return false; if (n == 2 || n == 3) @@ -50,15 +50,15 @@ bool isPrimeMem (long n, long* primes) { * where the 0-based ith element is true if i is prime * returns: number of primes in *primes_ptr * - * N.B. numOfPrimesUpper is an upper-bound of - * the prime-counting function, given by + * N.B. numOfPrimesUpper is an upper-bound of + * the prime-counting function, given by * n/ln(n) * 1.25506 * * runs in O(n * sqrt(n)/ln(n)) (not a tight bound) * */ -long listOfPrimes (long n, long** primes_ptr, bool** primesTable_ptr) { +long listOfPrimes(long n, long** primes_ptr, bool** primesTable_ptr) { *primesTable_ptr = calloc (n + 1, sizeof (bool)); if (n == 0 || n == 1) { @@ -66,23 +66,23 @@ long listOfPrimes (long n, long** primes_ptr, bool** primesTable_ptr) { return 0; } - long numOfPrimesUpper = (long) (n/log(n) * 1.25506); + long numOfPrimesUpper = (long) (n/log(n) * 1.25506); *primes_ptr = malloc (sizeof (long) * numOfPrimesUpper); long numOfPrimes = 0; for (long i = 0; i <= n; i++) { - if (isPrimeMem (i, *primes_ptr)) { + if (isPrimeMem(i, *primes_ptr)) { (*primes_ptr)[numOfPrimes] = i; (*primesTable_ptr)[i] = true; numOfPrimes++; } } if (numOfPrimes < numOfPrimesUpper) { - long* tmp = realloc (*primes_ptr, + long* tmp = realloc (*primes_ptr, sizeof (long) * numOfPrimes); if (tmp != NULL) *primes_ptr = tmp; } - + return numOfPrimes; } @@ -92,7 +92,7 @@ long listOfPrimes (long n, long** primes_ptr, bool** primesTable_ptr) { * runs in O(n ln(ln(n))) apparently * */ -void sieveOfEratosthenes (int n, bool** primesTable_ptr) { +void sieveOfEratosthenes(int n, bool** primesTable_ptr) { *primesTable_ptr = malloc (sizeof (bool) * (n + 1)); for (int i = 0; i <= n; i++) (*primesTable_ptr)[i] = true; diff --git a/primes.h b/primes.h index dc4695b..bfb74f4 100644 --- a/primes.h +++ b/primes.h @@ -1,4 +1,6 @@ -bool isPrime (long n); -bool isPrimeMem (long n, long* primes); -long listOfPrimes (long n, long** primes_ptr, bool** primesTable_ptr); -void sieveOfEratosthenes (int n, bool** primesTable_ptr); +#include + +bool isPrime(long n); +bool isPrimeMem(long n, long* primes); +long listOfPrimes(long n, long** primes_ptr, bool** primesTable_ptr); +void sieveOfEratosthenes(int n, bool** primesTable_ptr); diff --git a/primes.o b/primes.o new file mode 100644 index 0000000000000000000000000000000000000000..0537a5363e6b39ff6cb8173842c8aff90b3c7dee GIT binary patch literal 2976 zcmb_dUuauZ7(X|eZSB@2lcIykh(5?H-CZhOVAemG^|~YIItD=m|1^y^Yq4o;?u~3> z>L4my!lb^{XF>KLGEijt;L;7(4PSyV$zvD{A_*8n!3c%<`_4Tly}eDu2S2#?ob&sB z-=Fh+Uv54!UU?~?X-tX6o@3i5g)%lfw7rjuZJdp;VV2;|Jzi`^xKpU}Vxi9LL0Yu@ z;8kKZX40WQ%55GRGwO8$upKXR$LR14qZJ^=VO}!o{0sh9p~amF|D>!;M|sg`Y0$R# zl+jtKUZuQl-`Ek+$=7y79Tri4{|rpR18z1!xFqAog@4Ni1lEX&c$dy-+)1?frO}@W z1y#f>yQs;cJwNhXD^^_qqcx$IX7FIwz{_(1Ap`d(o;}_E0!cZin8gq(eupw?p9( z^3@K#zOM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