Originally Posted by
robatino
You have to be careful since the division test has to go up to at least the EXACT square root of i. For example, 49 is composite, but only because it's divisible by 7, which is exactly the square root of 49. So you would at least have to replace "i < sqrt(a)" by "i <= sqrt(a)". I'm still not sure whether this is enough due to being unsure how the square root of an integer is calculated (for example, is sqrt(49) exactly 7, or could it be a little more or less, in which case the loop might miss the last value). You might have to do something like add a small constant like 0.5 to a before taking the square root. I don't know the most elegant way of handling this.
Having said this, it's definitely worthwhile, since for a near 1000000 you only have to go up to 1000, instead of 500000, for roughly a factor of 500 speedup for large a.