-
-
Notifications
You must be signed in to change notification settings - Fork 4.4k
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add eratosthenes sieve method for finding primes below given number #672
base: master
Are you sure you want to change the base?
Changes from 1 commit
6cdb91f
dc1cb7f
f50c966
2460c07
3bfa418
a3eb8ce
a702d15
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -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> | ||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Provide a brief description of the headers (what do they add).
Suggested change
|
||||||||||||||||||||||
|
||||||||||||||||||||||
/** | ||||||||||||||||||||||
* 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); | ||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. dynamically allocated memory must be freeed. For example, on line# 48, pointer to a new memory is returned. Hence, before the end of that function, there must be a |
||||||||||||||||||||||
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 | ||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||
* @return void | ||||||||||||||||||||||
*/ | ||||||||||||||||||||||
void test() | ||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||
{ | ||||||||||||||||||||||
/* 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]]); | ||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||||||||||||||||||||||
} | ||||||||||||||||||||||
|
||||||||||||||||||||||
/* Example Non-prime numbers */ | ||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. good code :) |
||||||||||||||||||||||
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 | ||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||
* @return None | ||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||
*/ | ||||||||||||||||||||||
int main() | ||||||||||||||||||||||
{ | ||||||||||||||||||||||
test(); | ||||||||||||||||||||||
return 0; | ||||||||||||||||||||||
} |
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. 🙂