দুইভাবে implement করছি একটা main ফাংশনে অপরটা user define function use করে
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<stdio.h> | |
#include<math.h> | |
#define N 1000001 | |
char p[N]; | |
int main() | |
{ | |
int i,j,root,k; | |
for(i=0;i<N;i++) | |
p[i]=1; // initialize array assume that all are prime | |
p[0]=p[1]=0; // zero and one are not prime | |
root=sqrt(N); // root max value of array | |
for(i=2;i<=root;i++) | |
if(p[i]==1){ // check the number’s multiple | |
for(j=2;i*j<=N;j++) | |
p[i*j]=0; // cross the multiple of i - composite number | |
} | |
// just print the prime numbers | |
for(k=2;k<=N;k++) | |
if(p[k]==1) | |
printf("%d ",k); | |
// printf("\n\n"); | |
return 0; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<stdio.h> | |
#include<math.h> | |
#define N 100001 | |
char P[N]; | |
void sieve( ); | |
int main() | |
{ | |
sieve(); | |
int i,j; | |
for(j=2;j<N;j++) | |
if(P[j]==1) | |
printf("%d ",j); | |
return 0; | |
} | |
void sieve( ) | |
{ | |
int i,j,root; | |
for(i=0;i<N;i++) | |
P[i]=1; // assume that all are prime number | |
P[0]=P[1]=0; // zero one are not prime | |
root=sqrt(N); | |
for(i=2;i<=root;i++) | |
if(P[i]==1){ | |
for(j=2;i*j<=N;j++) | |
P[i*j]=0; | |
} | |
} |
আরেকভাবে implement করছি সেটা হচ্ছে , একটা array তে প্রাইম নাম্বার গুলো বের করে আরেকটা array তে প্রাইম নাম্বার গুলো রাখছি।
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<stdio.h> | |
#include<math.h> | |
#define N 100000 | |
#define M 10000 | |
char P[N]; | |
int prime[M]; // all prime number in the | |
void sieve( ); | |
int main() | |
{ | |
sieve(); | |
int i,j; | |
for(j=0;j<M;j++){ | |
printf("%d \n",prime[j]); | |
} | |
return 0; | |
} | |
void sieve( ) | |
{ | |
int i,j,root; | |
for(i=0;i<N;i++) | |
P[i]=1; // assume that all are prime number | |
P[0]=P[1]=0; // zero one are not prime | |
root=sqrt(N); | |
for(i=2;i<=root;i++) | |
if(P[i]==1){ | |
for(j=2;i*j<=N;j++) | |
P[i*j]=0; | |
} | |
// tranfer prime number into another prime[] array | |
j=0; | |
for(i=2;i<N && j<M;i++){ | |
if(P[i]==1) | |
prime[j++]=i; | |
} | |
} |