Actually, I just thought of the most efficient method: do compute z, so you can use that for (x = 1; x <= z; x++) loop (note that it has
to be <=, not < - yet another mistake in your code), but don't compute it with a square root function! Instead, update it as you increase t!
Let me explain:
* We will use a second variable Z=(z+1)² which we update using only additions. We will use the fact that (z+2)²=(z+1)²+2(z+1)+1.
* start with t=1, z=1, Z=4
* increment t as follows:
That way, z will always be the floor of the square root of t and you don't have to compute a single square root or even multiplication!
Your algorithm will still suck, but at least it won't be because of the square root.
* We will use a second variable Z=(z+1)² which we update using only additions. We will use the fact that (z+2)²=(z+1)²+2(z+1)+1.
* start with t=1, z=1, Z=4
* increment t as follows:
t += 2;
if (t >= Z) {
z++;
Z += (z+z)+1;
}
That way, z will always be the floor of the square root of t and you don't have to compute a single square root or even multiplication!
Your algorithm will still suck, but at least it won't be because of the square root.

