Quick Find primes

Source: Internet
Author: User

Quick Find primes

time limit: + ms | Memory Limit: 65535 KB

Difficulty: 3

Describe

now give you a positive integer N , you need to quickly find out the 2.....N These numbers contain all the primes.

Input

give a positive integer number N (n<=2000000)
but N to be 0 when the program ends.
The test data does not exceed - Group

Output

will be 2~n the output of all primes in the range. Two numbers separated by a space

Sample input

5

10

11

0

Sample output

2 3 5

2 3 5 7

2 3 5) 7 11

Code:

#include <stdio.h>int a[2000001]={0};int Main () {    int i,j,n;    for (i=2;i<=2000000;i++)    {        if (a[i]==0) for          (j=i+i;j<=2000000;j+=i)         a[j]=1;      }    while (scanf ("%d", &n)!=eof) {if (n==0) break;else{           printf ("2");           for (i=3;i<=n;i++)           if (a[i]==0)           printf ("%d", I);           printf ("\ n");}    }    return 0;}        


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Quick Find primes

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.