(Hdu step 2.1.2) How many prime numbers (determine whether a number is a prime number)

Source: Internet
Author: User

(Hdu step 2.1.2) How many prime numbers (determine whether a number is a prime number)

Question:

How many prime numbers
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission (s): 8513 Accepted Submission (s): 2716
Problem DescriptionGive you a lot of positive integers, just to find out how many prime numbers there are.
InputThere 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
Authorwangye
SourceHDU 2007-11 Programming Contest_WarmUp
Recommend whisky

Question Analysis:

Determine whether a number is a prime number. This question can be done in many ways: the definition method, the Euler's sieve method, and the Elatos sieve method. Use this question

Definition method.


Some knowledge points about prime numbers:

First, for a natural number N, as long as it can be divisible by a non-1 Non-its own number, it is certainly not a prime number, so no
Other numbers must be removed.
Second, for N, you only need to remove the prime number less than N. For example, if N can be divisible by 15, the actual
3 and 5 can be divisible. If N cannot be divisible by 3 and 5, N will never be divisible by 15.
Third, for N, do not have to remove all prime numbers from 2 to N-1, just remove all prime numbers less than or equal to √ N (root N. This can be proved by the reverse proof:
If N is a combination, there must be integers d1 and d2 greater than 1 and less than N, so that N = d1 × d2.
If d1 and d2 are greater than √ N, there are: N = d1 × d2> √ N × √ N = N.
This is not possible. Therefore, one of d1 and d2 must be less than or equal to √ N.


The Code is as follows:

/** B. cpp ** Created on: January 30, 2015 * Author: Administrator */# include
 
  
# Include
  
   
# Include
   
    
Using namespace std;/*** determine whether a number is a prime number */bool isPrime (int n) {if (n = 1 | n = 0) {// 0 and 1 are neither sum nor Prime return false;} int I; for (I = 2; I <= sqrt (n * 1.0); ++ I) {// to determine whether a number is a prime number, you only need to go to the <= root number n. if n is obtained from the upper bound, TLEif (n % I = 0) {// if it can integer one of the factors return false; // It indicates that it is not a prime number} return true;} int main () {int n; while (scanf ("% d", & n )! = EOF) {int ans = 0; int I; for (I = 0; I <n; ++ I) {int temp; scanf ("% d ", & temp); if (isPrime (temp) = true) {ans ++ ;}} printf ("% d \ n", ans);} return 0 ;}
   
  
 







Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.