小于N的素数个数,欧拉筛选
public class Solution
{
public int countPrimes(int n)
{
boolean []flag = new int[n];
int res = 0;
for(int i=2; i<n; i++)
{
if(flag[i] == false)
{
res++;
for(int j=1; i*j<n; j++)
{
flag[i*j] = true;
}
}
}
return res;
}
}
计算有多少对质数和等于输入这个整数
class Solution
{
public static int getAnswer(int n)
{
boolean []dp = new boolean[n];
for(int i=2; i<n; i++)
{
if(dp[i]==true) continue;
for(int j=2; i*j<n; j++)
{
dp[i*j] = true;
}
}
int res = 0;
for(int i=2; i<=n/2; i++)
{
if(!dp[i] && !dp[n-i]) res++;
}
return res;
}
}