Count DePrimes
Count DePrimes |
A number is called a DePrime if the sum of its prime factors is a prime number.
Given a and b count the number of DePrimesxi such that axib.
Input
Each line contains a and b in the format ``a b". 2a5000000.ab5000000.
Input is terminated by a line containing `0'.
Output
Each line should contain the number of DePrimes for the corresponding test case.
Explanation:
In Test Case 2, take 10. Its Prime Factors are 2 and 5. Sum of Prime Factors is 7, which is a prime. So, 10 is a DePrime.
Sample Input
2 5 10 21 100 120 0
Sample Output
4 9 9
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.StreamTokenizer;
import java.util.Arrays;
import java.util.ArrayList;
public class Main {
public static final boolean DEBUG = false;
public static final int N = 5000001;
public StreamTokenizer tokenizer;
public BufferedReader cin;
public PrintWriter cout;
public int a, b;
public boolean[] vis = new boolean[N];
public ArrayList<Integer> vPrime = new ArrayList<Integer>();
public int[] f = new int[N];
public int[] sum = new int[N];
public void init()
{
try {
if (DEBUG) {
cin = new BufferedReader(new InputStreamReader(
new FileInputStream("d:\\OJ\\uva_in.txt")));
} else {
cin = new BufferedReader(new InputStreamReader(System.in));
}
cout = new PrintWriter(new OutputStreamWriter(System.out));
tokenizer = new StreamTokenizer(cin);
preprocess();
} catch (Exception e) {
e.printStackTrace();
}
}
public String next()
{
try {
tokenizer.nextToken();
if (tokenizer.ttype == StreamTokenizer.TT_EOF) return null;
else if (tokenizer.ttype == StreamTokenizer.TT_WORD) return tokenizer.sval;
else if (tokenizer.ttype == StreamTokenizer.TT_NUMBER) return String.valueOf((int)tokenizer.nval);
else return null;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
public boolean input()
{
a = Integer.parseInt(next());
if (a == 0) return false;
b = Integer.parseInt(next());
return true;
}
public void preprocess()
{
Arrays.fill(vis, false);
Arrays.fill(sum, 0);
vis[0] = vis[1] = true;
for (int i = 2; i < N; i++) {
if (!vis[i]) {
vPrime.add(i);
sum[i] = i;
}
for (int j = 0, size = vPrime.size(); j < size && i * vPrime.get(j) < N; j++) {
int tmp = vPrime.get(j);
vis[i * tmp] = true;
if (i % tmp == 0) {
sum[i * tmp] = sum[i];
break;
} else {
sum[i * tmp] = sum[i] + tmp;
}
}
}
f[0] = f[1] = 0;
for (int i = 2; i < N; i++) {
f[i] = f[i - 1] + (vis[sum[i]] == false ? 1 : 0);
}
}
public void solve()
{
int cnt = f[b] - f[a - 1];
cout.println(cnt);
cout.flush();
}
public static void main(String[] args) {
Main solver = new Main();
solver.init();
while (solver.input()) {
solver.solve();
}
}
}