Limits 1s, 512 MB

Sieve of Eratosthenes is a fast algorithm for finding prime numbers in larger ranges. According to Wikipedia, the algorithm works as follows:

To find all the prime numbers less than or equal to a given integer n by Eratosthenes' method:

  • Create a list of consecutive integers from 2 through n: (2, 3, 4, ..., n).
  • Initially, let p equal 2, the smallest prime number.
  • Enumerate the multiples of p by counting to n from 2p in increments of p, and mark them in the list (these will be 2p, 3p, 4p, ... ; the p itself should not be marked).
  • Find the first number greater than p in the list that is not marked. If there was no such number, stop. Otherwise, let p now equal this new number (which is the next prime), and repeat from step 3.

When the algorithm terminates, the numbers remaining not marked in the list are all the primes below n.

Your friend Shrabonti is very excited after learning this algorithm and even coded it by following the pseudo code. She also managed to successfully solve a problem with her code. But when she has found another problem and submitted the same code, she found wrong answer. She got very surprised with this and shared her code with you. Here's the code:

 1#include <stdio.h>
 2
 3int isPrime[ 1000100 ] ; 
 4void sieve() {
 5	for( int i = 2 ; i <= 1000000 ; i ++ ) {
 6		isPrime[ i ] = 1 ; 
 7	}	
 8	for( int i = 2 ; i <= 1000000 ; i ++ ) {
 9		for( int j = i * i ; j <= 1000000 ; j += i ) {
10			isPrime[ j ] = 0 ; 
11		}
12	}
13}
14int main() {
15	sieve() ; 
16	int N ; 
17	scanf("%d",&N) ; 
18	int num ; 
19	for( int i = 0 ; i < N ; i ++ ) {
20		scanf("%d",&num) ; 
21		if( isPrime[ num ] == 1 ) {
22			printf("%d is a prime number.\n",num);
23		}
24		else {
25			printf("%d is not a prime number.\n",num);
26		}
27	}
28	return 0 ; 
29}

She has also provided you the problem statement for her problem. It says, "You will be given some integer numbers from 1 to 10610^6. You have to tell whether each of those numbers are prime or not. The first line will contain an integer N, the amount of numbers that you will get. After that N lines follow, each line will have a number X from 1 to 10610^6. If X is a prime, just prime "X is a prime number", otherwise print "X is not a prime number".

So help Shrabonti debug the given code. Or you can write a code of your own that produces the correct results.

Sample

InputOutput
10
2
3
5
4
6
7
10
9
71
21
2 is a prime number.
3 is a prime number.
5 is a prime number.
4 is not a prime number.
6 is not a prime number.
7 is a prime number.
10 is not a prime number.
9 is not a prime number.
71 is a prime number.
21 is not a prime number.

Submit

Login to submit.

Statistics

81% Solution Ratio
fsshakkhorEarliest, Dec '16
user.2599Fastest, 0.0s
nusuBotLightest, 4.9 MB
ShuddhoShortest, 194B
Toph uses cookies. By continuing you agree to our Cookie Policy.