Add eratosthenes sieve method for finding primes below given number#672
Add eratosthenes sieve method for finding primes below given number#672anoopemacs wants to merge 7 commits intoTheAlgorithms:masterfrom
Conversation
Panquesito7
left a comment
There was a problem hiding this comment.
Well written and documented code. 😄 👍
Please enable GitHub Actions in your repository of this fork in this link: https://github.com/anoopemacs/C/actions
misc/sieve_of_eratosthenes.c
Outdated
| * Test function | ||
| * @return void | ||
| */ | ||
| void test() |
There was a problem hiding this comment.
| void test() | |
| static void test() |
misc/sieve_of_eratosthenes.c
Outdated
| } | ||
|
|
||
| /** | ||
| * Test function |
There was a problem hiding this comment.
| * Test function | |
| * @brief Test function |
misc/sieve_of_eratosthenes.c
Outdated
| } | ||
|
|
||
| /** | ||
| * Driver Code |
There was a problem hiding this comment.
| * Driver Code | |
| * @brief Driver Code |
misc/sieve_of_eratosthenes.c
Outdated
|
|
||
| /** | ||
| * Driver Code | ||
| * @return None |
There was a problem hiding this comment.
| * @return None | |
| * @returns 0 on exit |
misc/sieve_of_eratosthenes.c
Outdated
| @@ -0,0 +1,72 @@ | |||
| /** | |||
| * @file | |||
| * @brief Get list of prime numbers using Sieve of Eratosthenes | |||
There was a problem hiding this comment.
Provide a Wikipedia link in Markdown format (if possible).
If there's no Wikipedia link, provide another web source for algorithm explanation. 🙂
misc/sieve_of_eratosthenes.c
Outdated
| #include <assert.h> | ||
| #include <stdbool.h> | ||
| #include <stdio.h> | ||
| #include <stdlib.h> | ||
| #include <string.h> |
There was a problem hiding this comment.
Provide a brief description of the headers (what do they add).
| #include <assert.h> | |
| #include <stdbool.h> | |
| #include <stdio.h> | |
| #include <stdlib.h> | |
| #include <string.h> | |
| #include <assert.h> /// for assert | |
| #include <stdbool.h> /// | |
| #include <stdio.h> /// | |
| #include <stdlib.h> /// | |
| #include <string.h> /// |
Panquesito7
left a comment
There was a problem hiding this comment.
Amazing work, I like how you've made the code. 😄 👍
Code and documentation is pretty good and refined; LGTM. 🙂
|
Thank you very much @Panquesito7 for your detailed review and guidance! I really appreciate you taking the time to guide in such detail :) |
kvedala
left a comment
There was a problem hiding this comment.
👍 well written code. Please check the comments.
| */ | ||
| bool* sieve(int N) | ||
| { | ||
| bool* primep = calloc(N+1, 1); |
There was a problem hiding this comment.
dynamically allocated memory must be freeed.
In this case, a note should be added to the function that the function free() must be called on the output pointer after its use.
For example, on line# 48, pointer to a new memory is returned. Hence, before the end of that function, there must be a free(primep);
| for (size_t i = 0, size = sizeof(primers) / sizeof(primers[0]); i < size; | ||
| ++i) | ||
| { | ||
| assert(primep[primers[i]]); |
There was a problem hiding this comment.
the correct loop check would be:
for (size_t j = 0, p = 0; j < 100; j++)
{
if (j == primers[p]) // j is a known prime number
{
assert(prime[j]);
p++; // this variable is used to keep a track of the index of known prime numbers array
} else { // j is a known composite number
assert(!prime[j]);
}
}This will check that your function ensures that both primes and composites are classified correctly.
| assert(primep[primers[i]]); | ||
| } | ||
|
|
||
| /* Example Non-prime numbers */ |
There was a problem hiding this comment.
good code :)
but becomes redundant after the above suggested loop. You can keep this loop as well - I like it as it is a good exercise
Description of Change
Notes: Added 'Sieve of Eratosthenes' algorithm along with test cases for primes below 100