Skip to content

Instantly share code, notes, and snippets.

@lawliet89
Created November 12, 2012 23:24
Show Gist options
  • Save lawliet89/4062765 to your computer and use it in GitHub Desktop.
Save lawliet89/4062765 to your computer and use it in GitHub Desktop.
Prime Numbers (SPOJ)
// https://www.spoj.pl/problems/PRIME1/
//http://www.swageroo.com/wordpress/spoj-problem-2-prime-generator-prime1/
#include <iostream>
using namespace std;
int main(){
int count;
cin >> count;
while(count--){
int upper, lower;
cin >> lower >> upper;
int *notPrimes = new int[upper-lower+1];
for (int i = 0; i < upper-lower+1; i++)
notPrimes[i] = 0;
for (int p = 2; p*p <= upper; p++){
int i = lower/p;
i *= p;
for (; i <= upper; i += p){
if (i >= lower && i != p)
notPrimes[i-lower] = 1;
}
}
for (int i = lower; i <= upper; i++){
if (i != 1 && notPrimes[i-lower] == 0)
cout << i << endl;
}
delete[] notPrimes;
cout << endl;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment