Created
November 12, 2012 23:24
-
-
Save lawliet89/4062765 to your computer and use it in GitHub Desktop.
Prime Numbers (SPOJ)
This file contains 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
// 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