-
-
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 all commits
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](https://en.wikipedia.org/wiki/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> /// for 'assert' in 'test' function | ||
#include <stdbool.h> /// for 'bool' type in 'sieve' and 'test' 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 | ||
* @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; | ||
} | ||
|
||
/** | ||
* @brief Test function | ||
* @return void | ||
*/ | ||
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, | ||
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]]); | ||
} | ||
} | ||
|
||
/** | ||
* @brief Driver Code | ||
* @return 0 on exit | ||
*/ | ||
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.
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);