Skip to content

Commit

Permalink
Add problem PRIME1 from codechef
Browse files Browse the repository at this point in the history
  • Loading branch information
jhonber committed Feb 6, 2016
1 parent 780b217 commit 4b0bf15
Showing 1 changed file with 57 additions and 0 deletions.
57 changes: 57 additions & 0 deletions codechef/PRIME1.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#include<bits/stdc++.h>
using namespace std;

const int mx = 1e9 + 10;
vector<int> primes;

void gen() {
primes.push_back(2);
for (int p = 3; p * p <= mx; p += 2) {
int i = 2;
int f = true;
while (i * i <= p) {
if (p % i == 0) {
f = false;
break;
}
i ++;
}

if (f) primes.push_back(p);
}
}

bool isPrime(int x) {
if (x == 2) return true;

int i = 0;
while (i < primes.size() && primes[i] * primes[i] <= x) {
if (x % primes[i] == 0) return false;
i ++;
}
return true;
}

int main() {
gen();
int t;
scanf("%d", &t);

int n, m;
while (t --> 0) {
scanf("%d %d", &n, &m);

if (n <= 2) {
printf("2\n");
n = 3;
}

if (n % 2 == 0) n ++;
for (int i = n; i <= m; i += 2) {
if (isPrime(i)) printf("%d\n", i);
}
printf("\n");
}

return 0;
}

0 comments on commit 4b0bf15

Please sign in to comment.