Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 58ff849

Browse files
author
RMPR
committed
Implement sieve of Eratosthenes in C
1 parent 5250b05 commit 58ff849

File tree

1 file changed

+33
-0
lines changed
  • Algorithms/Maths/Sieve of Eratosthenes algorithm

1 file changed

+33
-0
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
4+
typedef enum Boolean {FALSE = 0, TRUE = 1} Boolean;
5+
6+
int main (int argc, char **argv)
7+
{
8+
int N = 0, i = 0, j = 0;
9+
int* numbers = NULL;
10+
Boolean *sieve;
11+
N = strtol(argv[1], NULL, 10);
12+
13+
if (N < 2) printf("\n No prime numbers \n\n");
14+
else
15+
{
16+
sieve = calloc(N -1, sizeof(Boolean));
17+
numbers = malloc( (N -1)*sizeof(int) );
18+
19+
for (i=2; i<=N; i++)
20+
numbers[i-2] = i;
21+
22+
for( i=0; i <= (N/2); i++ )
23+
for(j=i+1; j<=N -2; j++)
24+
if( (numbers[j] % numbers[i]) == 0 )
25+
sieve[j] = TRUE;
26+
27+
for(i=0; i<N -1; i++)
28+
if(!sieve[i])
29+
printf(" %d ", numbers[i]);
30+
printf("\n\n");
31+
}
32+
return 0;
33+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /