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 dc0dfda

Browse files
authored
Merge pull request fnplus#605 from RMPR/ft-sieve-of-erastosthene
Ft sieve of erastosthene
2 parents 4403b83 + 8f2c3b5 commit dc0dfda

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
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+
}

‎README.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ See [Extended Contributing Guidelines](https://github.com/fnplus/interview-techd
5858
| [Perfect number](https://en.wikipedia.org/wiki/Perfect_number) | :octocat: | :octocat: | :octocat: | :octocat: |
5959
| [Magic number](https://en.wikipedia.org/wiki/Magic_number) | | | | :octocat: |
6060
| [Polite number](https://en.wikipedia.org/wiki/Polite_number) | | :octocat: | :octocat: | :octocat: |
61-
| [Sieve of Eratosthenes](https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes) | | :octocat: | :octocat: | |
61+
| [Sieve of Eratosthenes](https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes) | :octocat: | :octocat: | :octocat: | |
6262
| [Modular exponentiation](https://en.wikipedia.org/wiki/Modular_exponentiation) | :octocat: | :octocat: | :octocat: | :octocat: |
6363

6464
| Searching and Sorting | C | CPP | Java | Python |

0 commit comments

Comments
(0)

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