Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork4.7k
Add eratosthenes sieve method for finding primes below given number#672
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
base:master
Are you sure you want to change the base?
Uh oh!
There was an error while loading.Please reload this page.
Conversation
Panquesito7 left a comment
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 enableGitHub 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.
| voidtest() | |
| staticvoidtest() |
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.
| *Testfunction | |
| *@briefTestfunction |
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.
| *DriverCode | |
| *@briefDriverCode |
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.
| * @returnNone | |
| * @returns0onexit |
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>/// |
Panquesito7 left a comment
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. 🙂
anoopemacs commentedOct 18, 2020
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.
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 memorymust be freeed.
In this case, a note should be added to the function that the functionfree() 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 afree(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_tj=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 numberassert(!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
Uh oh!
There was an error while loading.Please reload this page.
Description of Change
Notes: Added 'Sieve of Eratosthenes' algorithm along with test cases for primes below 100