From 6cdb91f44821cadcdde3632f27bac2e1bb71b474 Mon Sep 17 00:00:00 2001
From: anoopemacs <anoopemacs@gmail.com>
Date: Sun, 18 Oct 2020 01:44:01 +0530
Subject: [PATCH 1/7] Add eratosthenes sieve method for finding primes below
 given number

---
 misc/sieve_of_eratosthenes.c | 72 ++++++++++++++++++++++++++++++++++++
 1 file changed, 72 insertions(+)
 create mode 100644 misc/sieve_of_eratosthenes.c

diff --git a/misc/sieve_of_eratosthenes.c b/misc/sieve_of_eratosthenes.c
new file mode 100644
index 0000000000..1de74e9011
--- /dev/null
+++ b/misc/sieve_of_eratosthenes.c
@@ -0,0 +1,72 @@
+/**
+ * @file
+ * @brief Get list of prime numbers using Sieve of Eratosthenes
+ * @details
+ * Sieve of Eratosthenes is an algorithm that finds all the primes
+ * between 2 and N.
+ *
+ * Time Complexity  : \f$O(N \cdot\log \log N)\f$
+ * <br/>Space Complexity : \f$O(N)\f$
+ *
+ */
+
+#include <assert.h>
+#include <stdbool.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+/**
+ * Return all primes between 2 and the given number
+ * @param N the largest number to be checked for primality
+ * @return is_prime a dynamically allocated array of `N + 1` booleans identifying if `i`^th number is a prime or not
+ */
+bool* sieve(int N)
+{
+    bool* primep = calloc(N+1, 1);
+    memset(primep, true, N+1); 
+    primep[0] = false; //0 is not a prime number
+    primep[1] = false; //1 is not a prime number
+    
+    int i, j;
+    for (i=2; i!=N/2; ++i)// i!=N+1 also works
+        for (j=2; j<=N/i; ++j)// i*j <= N also works
+            primep[i*j] = false;
+
+    return primep;
+}
+
+/**
+ * Test function
+ * @return void
+ */
+void test()
+{
+    /* all the prime numbers less than 100 */
+    int primers[] = {2,  3,  5,  7,  11, 13, 17, 19, 23, 29, 31, 37, 41,
+                     43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97};
+    bool* primep = sieve(100);
+    for (size_t i = 0, size = sizeof(primers) / sizeof(primers[0]); i < size;
+         ++i)
+    {
+        assert(primep[primers[i]]);
+    }
+
+    /* Example Non-prime numbers */
+    int NonPrimers[] = {4, 6, 8, 9, 10, 12, 16, 51};
+    for (size_t i = 0, size = sizeof(NonPrimers) / sizeof(NonPrimers[0]);
+         i < size; ++i)
+    {
+        assert(!primep[NonPrimers[i]]);
+    }
+}
+
+/**
+ * Driver Code
+ * @return None
+ */
+int main()
+{
+    test();
+    return 0;
+}

From dc1cb7f55a1f9f20d16816c83297a07d570e0fed Mon Sep 17 00:00:00 2001
From: anoopemacs <anoopemacs@gmail.com>
Date: Mon, 19 Oct 2020 00:25:41 +0530
Subject: [PATCH 2/7] Add Wikipedia link explaining the Sieve of Eratosthenes
 alogrithm

---
 misc/sieve_of_eratosthenes.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/misc/sieve_of_eratosthenes.c b/misc/sieve_of_eratosthenes.c
index 1de74e9011..dda2b8946e 100644
--- a/misc/sieve_of_eratosthenes.c
+++ b/misc/sieve_of_eratosthenes.c
@@ -1,6 +1,7 @@
 /**
  * @file
- * @brief Get list of prime numbers using Sieve of Eratosthenes
+ * @brief Get list of prime numbers using [Sieve of
+ * Eratosthenes](https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes)
  * @details
  * Sieve of Eratosthenes is an algorithm that finds all the primes
  * between 2 and N.

From f50c966293db7677bf448b61e777ab8f33bbbf50 Mon Sep 17 00:00:00 2001
From: anoopemacs <anoopemacs@gmail.com>
Date: Mon, 19 Oct 2020 00:30:59 +0530
Subject: [PATCH 3/7] Fix the three commenting mistakes pointed out by
 @Panquesito7

---
 misc/sieve_of_eratosthenes.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/misc/sieve_of_eratosthenes.c b/misc/sieve_of_eratosthenes.c
index dda2b8946e..0b228d5fca 100644
--- a/misc/sieve_of_eratosthenes.c
+++ b/misc/sieve_of_eratosthenes.c
@@ -38,10 +38,10 @@ bool* sieve(int N)
 }
 
 /**
- * Test function
+ * @brief Test function
  * @return void
  */
-void test()
+static void test()
 {
     /* all the prime numbers less than 100 */
     int primers[] = {2,  3,  5,  7,  11, 13, 17, 19, 23, 29, 31, 37, 41,
@@ -63,8 +63,8 @@ void test()
 }
 
 /**
- * Driver Code
- * @return None
+ * @brief Driver Code
+ * @return 0 on exit
  */
 int main()
 {

From 2460c07ccb6eccca06c380e6eb8098b09cce4e09 Mon Sep 17 00:00:00 2001
From: github-actions <${GITHUB_ACTOR}@users.noreply.github.com>
Date: Sun, 18 Oct 2020 19:03:53 +0000
Subject: [PATCH 4/7] updating DIRECTORY.md

---
 DIRECTORY.md | 1 +
 1 file changed, 1 insertion(+)

diff --git a/DIRECTORY.md b/DIRECTORY.md
index e9c9426f55..665ce584a5 100644
--- a/DIRECTORY.md
+++ b/DIRECTORY.md
@@ -254,6 +254,7 @@
   * [Prime Factoriziation](https://github.com/TheAlgorithms/C/blob/master/misc/prime_factoriziation.c)
   * [Quartile](https://github.com/TheAlgorithms/C/blob/master/misc/quartile.c)
   * [Rselect](https://github.com/TheAlgorithms/C/blob/master/misc/rselect.c)
+  * [Sieve Of Eratosthenes](https://github.com/TheAlgorithms/C/blob/master/misc/sieve_of_eratosthenes.c)
   * [Strong Number](https://github.com/TheAlgorithms/C/blob/master/misc/strong_number.c)
   * [Sudoku Solver](https://github.com/TheAlgorithms/C/blob/master/misc/sudoku_solver.c)
   * [Tower Of Hanoi](https://github.com/TheAlgorithms/C/blob/master/misc/tower_of_hanoi.c)

From 3bfa418a0e73dcd408cb2a63dd5d40b04f4e65b6 Mon Sep 17 00:00:00 2001
From: anoopemacs <anoopemacs@gmail.com>
Date: Mon, 19 Oct 2020 00:47:09 +0530
Subject: [PATCH 5/7] Change variable name to convention, from 'NonPrimers' to
 'nonPrimers'

---
 misc/sieve_of_eratosthenes.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/misc/sieve_of_eratosthenes.c b/misc/sieve_of_eratosthenes.c
index 0b228d5fca..8acc062623 100644
--- a/misc/sieve_of_eratosthenes.c
+++ b/misc/sieve_of_eratosthenes.c
@@ -54,11 +54,11 @@ static void test()
     }
 
     /* Example Non-prime numbers */
-    int NonPrimers[] = {4, 6, 8, 9, 10, 12, 16, 51};
-    for (size_t i = 0, size = sizeof(NonPrimers) / sizeof(NonPrimers[0]);
+    int nonPrimers[] = {4, 6, 8, 9, 10, 12, 16, 51};
+    for (size_t i = 0, size = sizeof(nonPrimers) / sizeof(nonPrimers[0]);
          i < size; ++i)
     {
-        assert(!primep[NonPrimers[i]]);
+        assert(!primep[nonPrimers[i]]);
     }
 }
 

From a3eb8ce8c2000551302809f4257fe21cd2d7f6df Mon Sep 17 00:00:00 2001
From: anoopemacs <anoopemacs@gmail.com>
Date: Mon, 19 Oct 2020 00:59:06 +0530
Subject: [PATCH 6/7] Explain the need for each header and remove unused header
 <stdio.h>

---
 misc/sieve_of_eratosthenes.c | 9 ++++-----
 1 file changed, 4 insertions(+), 5 deletions(-)

diff --git a/misc/sieve_of_eratosthenes.c b/misc/sieve_of_eratosthenes.c
index 8acc062623..dc222abbd0 100644
--- a/misc/sieve_of_eratosthenes.c
+++ b/misc/sieve_of_eratosthenes.c
@@ -11,11 +11,10 @@
  *
  */
 
-#include <assert.h>
-#include <stdbool.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
+#include <assert.h> /// for 'assert' in 'test' function
+#include <stdbool.h> /// for 'bool' type in 'sieve' and 'test' function
+#include <stdlib.h> /// for 'memset' in 'sieve' function
+#include <string.h> /// for 'calloc' in 'sieve' function
 
 /**
  * Return all primes between 2 and the given number

From a702d1507a05a1257abc17fc996846162367a072 Mon Sep 17 00:00:00 2001
From: anoopemacs <anoopemacs@gmail.com>
Date: Mon, 19 Oct 2020 01:01:30 +0530
Subject: [PATCH 7/7] Fix a mistake in header file need explaination comment

---
 misc/sieve_of_eratosthenes.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/misc/sieve_of_eratosthenes.c b/misc/sieve_of_eratosthenes.c
index dc222abbd0..963541f280 100644
--- a/misc/sieve_of_eratosthenes.c
+++ b/misc/sieve_of_eratosthenes.c
@@ -13,8 +13,8 @@
 
 #include <assert.h> /// for 'assert' in 'test' function
 #include <stdbool.h> /// for 'bool' type in 'sieve' and 'test' function
-#include <stdlib.h> /// for 'memset' in 'sieve' function
-#include <string.h> /// for 'calloc' in 'sieve' function
+#include <stdlib.h> /// for 'calloc' in 'sieve' function
+#include <string.h> /// for 'memset' in 'sieve' function
 
 /**
  * Return all primes between 2 and the given number