Skip to content

Instantly share code, notes, and snippets.

@fayimora
Created February 2, 2012 02:17
Show Gist options
  • Save fayimora/1720960 to your computer and use it in GitHub Desktop.
Save fayimora/1720960 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <cmath>
#include <cstring>
using namespace std;
void printPrimes(int start, int finish)
{
int length = finish+1;
int upperBoundSqrt = (int) sqrt((double)finish); // compute limit to marking off primes
bool primes[length];
// memset(primes, true, sizeof(bool) * length);
for(int i=0; i<length; i++) primes[i]=true;
for(int i=2; i<=upperBoundSqrt; i++)
{
if(primes[i] && i>=start)
cout << i << endl; //print out prime before marking off its multiples
for(int j=i; j<=finish; j+=i) primes[j]=false;
}
for(int i=upperBoundSqrt; i<=finish; i++)
if(primes[i])
cout << i << endl;
}
int main(void)
{
int number, start, finish;
cin >> number;
for(int i=0; i<number; i++)
{
cin >> start;
cin >> finish;
printPrimes(start, finish);
if(i+1!=number) cout << endl;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment