Skip to main content

Posts

Showing posts with the label SieveOfEratosthenes

SieveOfEratosthenes

To Get the prime number in the range 2.N , you can use a simple and intersting method known as Sieve Of Eratosthenes. For Exampl this Program :- #include <iostream.h> #include <conio.h> #define Max 100 void main() { clrscr(); int isprime[Max+1]; int n,i; cout<<"\tEnter N (<=100):\t"; cin>>n; for(i=2;i<=n;i++) isprime[i]=1; for(i=2;i*i<=n;i++) { if (isprime[i]==1) {for(int j=i;i*j<=n;j++) isprime[i*j]=0; } } int gap =0; int bestgap=0; int right=0; for(i=2;i<=n;i++) { if (isprime[i])gap=0; else gap++; if (gap>bestgap) { bestgap=gap; right=i; } } int left=right-bestgap+1; cout<<"\t\nThere are no prime between"<<left<<"\tand"<<right<<endl; cout<<"\t\nThat is"<<bestgap<<"\tconsecutive integers"<<endl; getch(); }