Almost Prime
Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u
Description
A number is called almost prime if it has exactly two distinct prime divisors. For example, numbers 6, 18, 24 are almost prime, while 4, 8, 9, 42 are not. Find the amount of almost prime numbers which are between 1 and n, inclusive.
Input
Input contains one integer number n (1 ≤ n ≤ 3000).
Output
Output the amount of almost prime numbers between 1 and n, inclusive.
Sample Input
Input
10
Output
2
Input
21
Output
8
思路:
由给出的范围可得素数范围:0~1500 (3000/2=1500);
筛法得到0~x/2(x为所给值)之间的素数;
理解题意:题目要求的一类殆素数的特征是:仅由两个素数通过乘积或乘方变化的来 因此按序模素数只有两个能被整除则说明该数符合题意,ans++;
#include<cstdio>
#include<iostream>
using namespace std;
int prime[1501];
int main()
{
int x;
while(scanf("%d",&x)!=EOF)
{
int i,j;
prime[0]=0;
prime[1]=0;
for(i=2;i<x/2+1;i++) prime[i]=1;
for(i=1;i<x/2;i++)
{
if(prime[i]) for(j=2*i;j<x/2+1;j+=i) prime[j]=0;
}
int num,count,ans=0;
for(num=6;num<=x;num++)
{
count=0;
for(j=0;j<num/2+1;j++)
{
if(prime[j]==0)continue;
if(num%j==0) count++;
if(count==3) break;}
if(count==2) ans++;
}
printf("%d\n",ans);
}
return 0;
}