Hello!I was trying to write a program that shows all the prime numbers smaller than n.However,something strange happens.When n is 10,for example,the program shows "2 5 7" (missing 3). If n is 100, the program shows all the prime numbers smaller than 100 except 3,5 and 7.What am I doing wrong?
for(i=3;i<=((int)sqrt((double)n));i=i+2)
if (j%i==0){ t=0;
break;}
For every n that is higher than 3, this code will automatically leave out the number 3 (because i is initialized as 3, and if j is 3, the if loop will be true and t will be set to 0 so that j (which is 3) will not be printed out.
IMHO, this is a not a good prime number generator algorithm, but then again I can't see the idea in your head, so you might be on to something that I can't see.
In fact, it will leave out every prime number lower than square root of n.
That's why when you enter 100, you get all prime numbers except 3, 5, and 7 (since square root of 100 is 10).