I learned a new prime screening, which is different from the previous one!

Source: Internet
Author: User
Prime Number Time Limit: 100 ms memory limit: 65536 k any questions? Click Here ^_^ Description

Evaluate the number of all prime numbers smaller than N.

Input

Multiple Input groups. Enter an integer n (n <1000000) and end with 0.

Output

Returns the number of all prime numbers within n.

Sample Input
100
Sample output
4
Prompt

Taking this question as an example, we need to find the prime number within n, n <= 1000000.

To save time, use the prime number sieve to mark all the prime numbers within 1000000!

 

Core algorithm code of this prime Screening:

After this code is run, the prime number is marked as 0, not marked as 1. Data processing is complete!

Int f [1000004]; int I, j; memset (F, 0, sizeof (f); F [1] = 1; // If the marker 1 is not a prime number, the marker 0 is a prime number I = 2; while (I <= 500000) {for (j = I * 2; j <= 1000000; J + = I) {f [J] = 1;} I ++; while (F [I] = 1) {I ++ ;}}

 

 

 

 

# Include <stdio. h> # include <string. h> int f [1000004]; int main () {int N; int I, j; memset (F, 0, sizeof (f); F [1] = 1; // flag is not I = 2; while (I <= 500000) {for (j = I * 2; j <= 1000000; j + = I) {f [J] = 1;} I ++; while (F [I] = 1) {I ++;} int K, CNT; while (scanf ("% d", & N) & n! = 0) {CNT = 0; For (k = 1; k <n; k ++) {If (F [k] = 0) CNT ++ ;} printf ("% d \ n", CNT);} return 0 ;}

 

I learned a new prime screening, which is different from the previous one!

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.