-
-
Notifications
You must be signed in to change notification settings - Fork 4.5k
Add eratosthenes sieve method for finding primes below given number #672
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
base: master
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
void test() | |
static void test() |
misc/sieve_of_eratosthenes.c
Outdated
} | ||
|
||
/** | ||
* Test function |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
* Test function | |
* @brief Test function |
misc/sieve_of_eratosthenes.c
Outdated
} | ||
|
||
/** | ||
* Driver Code |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
* Driver Code | |
* @brief Driver Code |
misc/sieve_of_eratosthenes.c
Outdated
|
||
/** | ||
* Driver Code | ||
* @return None |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
* @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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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> /// |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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 :) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍 well written code. Please check the comments.
*/ | ||
bool* sieve(int N) | ||
{ | ||
bool* primep = calloc(N+1, 1); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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