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