So i have the following problem: I need to cover the whole interval from $1$ to $10^{12}$ on a computer program. But i can´t do it on a for loop because it would be too slow. So can i iterate over a smaller amount ($\sqrt{n}$ for instance), and still cover the whole interval?
What i've done so far:
Iterate from $1$ to $\sqrt{n}$, then cover $(n-i)$, $\frac{n}{i}$,$\frac{n}{n-i}$.
But i'm still missing some numbers.
Thanks in advance.
If you have to examine every one of those numbers you will have to execute some code $10^{12}$ times. It doesn't matter how cleverly you organize the loop.
Some languages (for example, Python) can optimize looping for you by clever indexing ("vectorizing") that saves a little of the looping overhead - but not the essential calculations.
Edit in response to comment. If you want to look only at primes in that range, consider reading this file:
http://compoasso.free.fr/primelistweb/page/prime/liste_online_en.php