Because you start with index 1 - and it's zero, you set it that way. That means it's "not prime" so you ignore it.
When you enter loop, your index is 2 - and that's still one, so it's prime.
You enter the inner loop, which sets every multiple of the index to zero to indicate they are not prime. Because 2 is the first even number (by definition) that removes all even numbers from your sieve when you set
prime[i*j]=0;
So after the first iteration round your outer loop, 1 is not prime, 2 is prime, 3 is prime, 4 isn't, 5 is, and so on. Which is the pattern you are seeing.
I dont understand the inner loop here in first iteration it make all even number become zero? For(j=i;i*j<10000;j++) {prime[i*j]=0} Index i=2 when enter the loop I=j=2 2*2=4<10000 J++ This mean start j=2 then multiply it j=2*2 if it under 10000 then increment j with 1, so after it is 3*3=9(?) i dont understand this part!!
When you write a
for
loop, it has four parts:
for (initialization; condition; modification)
execution statement
The
execution statement
is obvious - it's the code that is executed each time you go round the loop. In your case, it's this:
{prime[i*j]=0;}
The
initialization
lets you set up starting values for the loop:
j = 1
The
condition
if tested each time you are about to enter the loop, and if it is true, the loop continues. If it is false, the loop terminates without executing the
execution statement
again.
i*j<10000
The
modification
if executed at the end of each execution of the
execution statement
to set the loop up for the next iteration:
j++
So you start by setting
j
to the value of i, and repeat the loop until
i * j
exceeds (or equals) 10000.
Make sense?
ah got it!!!! it means i=2 because the initialization and j =2 because the condition is true 4<10000 it will execute the execution statement then j will be incremented to 3 so i*j<10000 -> 6 <10000 that is why all the value of even number in array become 0 then for i=j=3 it will make the value of multiple 3 become 0 in array and so on?
Dat's de bunny! :thumbsup:
thankyou so much!!!!!!!!!!!!!!!!!!!!!!!!! for the detailed explanation!!!!!!!!!!!!!! hope to find someone like you again everytime im confuse!!!lol
You're welcome!