mirror of
https://github.com/TheAlgorithms/C
synced 2024-11-25 06:49:36 +03:00
feat: created prime_seive.c (#708)
* created prime_seive.c This function counts the number of prime numbers in O(nlogn) time. * Apply suggestions from code review Co-authored-by: David Leal <halfpacho@gmail.com> * updating DIRECTORY.md * updated prime_seive.c * Update misc/prime_seive.c Co-authored-by: David Leal <halfpacho@gmail.com> * added more changes please take a look at it * changed 1000000 to MAX_SIZE * updated line 10 * changed the code back to original * eliminated the problem of MAX_SIZE * added for loop to initialise all elements to 0 * made the changes * changed the code back to original i have changed my code back to original as some tests were failing * removed extra spaces & edited some lines * added new global variable * added extra space * added parameter & return statement Co-authored-by: David Leal <halfpacho@gmail.com> Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com>
This commit is contained in:
parent
aef9d8d53f
commit
b2def5ca0e
@ -254,6 +254,7 @@
|
||||
* [Poly Add](https://github.com/TheAlgorithms/C/blob/master/misc/poly_add.c)
|
||||
* [Prime](https://github.com/TheAlgorithms/C/blob/master/misc/prime.c)
|
||||
* [Prime Factoriziation](https://github.com/TheAlgorithms/C/blob/master/misc/prime_factoriziation.c)
|
||||
* [Prime Seive](https://github.com/TheAlgorithms/C/blob/master/misc/prime_seive.c)
|
||||
* [Quartile](https://github.com/TheAlgorithms/C/blob/master/misc/quartile.c)
|
||||
* [Rselect](https://github.com/TheAlgorithms/C/blob/master/misc/rselect.c)
|
||||
* [Strong Number](https://github.com/TheAlgorithms/C/blob/master/misc/strong_number.c)
|
||||
|
77
misc/prime_seive.c
Normal file
77
misc/prime_seive.c
Normal file
@ -0,0 +1,77 @@
|
||||
/**
|
||||
* @file
|
||||
* @brief [Prime Seive](https://leetcode.com/problems/count-primes/)
|
||||
* algorithm implementation.
|
||||
* @author [Divyansh Kushwaha](https://github.com/webdesignbydivyansh)
|
||||
*/
|
||||
#include <assert.h> /// for assert
|
||||
#include <stdio.h> /// for standard input output
|
||||
#include <stdlib.h> /// for general purpose standard library
|
||||
|
||||
const unsigned long long MAX_SIZE = 1000000; /// variable upto which prime numbers are to be found out
|
||||
/**
|
||||
* @addtogroup misc
|
||||
* @{
|
||||
*/
|
||||
/**
|
||||
* @brief Prime Sieve works in O(nlogn) time
|
||||
* @param p array to be updated
|
||||
* @returns void
|
||||
*/
|
||||
void prime(int *p)
|
||||
{
|
||||
for(long long int i=3;i<=MAX_SIZE;i+=2) { p[i]=1; }
|
||||
for(long long int i=3;i<=MAX_SIZE;i+=2)
|
||||
{
|
||||
if(p[i]==1) {
|
||||
for(long long int j=i*i;j<=MAX_SIZE;j+=i) {
|
||||
p[j]=0;
|
||||
}
|
||||
}
|
||||
}
|
||||
p[2]=1;
|
||||
p[0]=p[1]=0;
|
||||
}
|
||||
/**
|
||||
* @brief Count func counts the number of
|
||||
* prime numbers.
|
||||
* @param arr contains the prime numbers
|
||||
* @param size denotes upto which prime numbers are to be found out
|
||||
* @returns count of prime numbers
|
||||
*/
|
||||
int count(int *arr, const int size){
|
||||
int k=0;
|
||||
for(int i=0;i<=size;i++){
|
||||
if(arr[i]==1){
|
||||
k++;
|
||||
}
|
||||
}
|
||||
return k;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Test implementations
|
||||
* @returns void
|
||||
*/
|
||||
static void test()
|
||||
{
|
||||
// Test Case 1
|
||||
const int size = 10; /* array size */
|
||||
printf("Test Case 1...");
|
||||
int arr[1000005]={0}; /* array to store prime numbers */
|
||||
prime(arr);
|
||||
assert(count(arr,size)==4);
|
||||
printf("Passed\n");
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Main function
|
||||
* @param argc commandline argument count (ignored)
|
||||
* @param argv commandline array of arguments (ignored)
|
||||
* @returns 0 on exit
|
||||
*/
|
||||
int main(int argc, const char *argv[])
|
||||
{
|
||||
test(); // execute the tests
|
||||
return 0;
|
||||
}
|
Loading…
Reference in New Issue
Block a user