সিভের অ্যালগরিদম দিয়ে প্রবলেম টি সল্ভে করছি, তাই সিভের অ্যালগরিদম আগে শিখে নেন। প্রবলেমটির মাঝে কোন জটিলতা নাই খুবই সিম্পল প্রবলেম। কোডে যথেষ্ট পরিমাণ কমেন্ট-আউট করছি বোঝার সুবিধার জন্য।
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 1000000 | |
char p[N]; | |
int main() | |
{ | |
int i,j,root,k,nth_prime=0; | |
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 is the number is prime | |
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) | |
nth_prime++; | |
//printf("%d ",k); | |
if(nth_prime==10001){ | |
printf("%d ",k); | |
break; | |
} | |
// printf("\n\n"); | |
} | |
return 0; | |
} | |