How many prime numbers
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 22283 Accepted Submission(s): 7523
Problem Description
Give you a lot of positive integers, just to find out how many prime numbers there are.
Input
There are a lot of cases. In each case, there is an integer N representing the number of integers to find. Each integer won’t exceed 32-bit signed integer, and each of them won’t be less than 2.
Output
For each case, print the number of prime numbers you have found out.
Sample Input
32 3 4
Sample Output
2
Author
wangye
- AC Code
#include <cstdio> #include <cstring> #include <cmath> #include <algorithm> using namespace std; bool prime(long long x) { if(x==2 || x==3) return 1; //2,3是特例,虽然不在6的附近,但是是素数 if(x%6 != 1 && x%6 != 5) return 0; //如果不在6的倍数附近,肯定不是素数 else //对6倍数附近的数进行判断 { for(long long i=5; i<=sqrt(x); i=i+6) { if(x%i==0 || x%(i+2)==0) { return 0; break; } } return 1; } } int main() { long long n; while(~scanf("%lld",&n)) { long long x, sum = 0; for(long long i=1; i<=n; i++) { scanf("%lld",&x); if(prime(x)) sum++; } printf("%d\n",sum); } return 0; }