# How do I find a formula to find the even squares between two numbers? [closed]

I have a coding assignment where I have to take two numbers, and print all the even square numbers between them.

like this:

(1, 100)

4, 16, 36, 64, 100

I want to find a math formula to do this. I have tried looking on other threads, but they were poorly explained or the solutions didn't work like this thread: How to find all perfect squares in a given range of numbers?

How do I find this? No code please.

• If $A$ is the first number just take the square root of $\frac A4$ and round up and call it $n$ and if $B$ is the second number just take the square root of $\frac B4$ and round down and call it $m$. Then the even perfect squares between $A$ and $B$ will be $4n^2, 4(n+1)^2, ....., 4(m-1)^2, 4m^2$. Oct 6, 2022 at 22:57

Let's say you have to find all even squares between integers $$a$$ and $$b$$ (included), with $$a \le b$$, i.e. all even squares in $$[a,b]$$. I'll use informal English rather than math notation, in case you are not familiar with math notation. In case you prefer to have math notation, please tell.

Take:
$$c =$$ integer part of $$\sqrt a$$
$$d =$$ lowest even integer greater than or equal to $$c$$
Test if $$d^2 = a$$. If this is the case, $$d^2$$ is your first result number.
In all cases, increase $$d$$ by $$2$$.
Test whether the new $$d^2$$ is lower or equal to $$b$$. If true, this is a new result number. Loop until $$d^2 > b$$.

Example: all even squares between 50 and 150, included.
$$c =$$ integer part of $$\sqrt {50}$$ $$= 7$$
$$d=8$$
$$d^2$$ is greater than $$50$$ so we test whether $$d^2$$ is lower or equal to $$150$$. This is the case, so $$8^2 = 64$$ is the first number we want.
Then we increase $$d$$ by $$2$$ which gives $$10$$.
$$10^2 = 100$$ which is lower or equal to $$150$$ so that is the second number we want.
Then $$12^2 = 144$$ is accepted too. The next one is $$14^2 = 196$$ which is greater than $$150$$ so we stop at $$144$$.

As a variant, you may notice that the sequence of even $$d^2$$ increases by $$12, 20, 28, 36$$ etc.: the difference is a sequence that increases $$8$$ by $$8$$.
This is because $$(2n+2)^2 - (2n)^2 = 8n + 4$$.
So when you take $$d =$$ lowest even integer greater than or equal to $$c$$, compute $$d^2$$, then to get the next $$d^2$$ increase by $$4d+4$$.

Example: above we had the first accepted number $$8^2 = 64$$. So $$d=8$$. Add $$4d+4 = 36$$ to $$64$$, that gives $$100$$ which is also accepted.
Then add $$4 \times 10 + 4 = 44$$ to $$100$$, that is $$144$$, accepted also.
Then add $$4 \times 12 + 4 = 52$$ to $$144$$, that is $$196$$, greater than $$150$$ so we stop at the previous one.

Suppose you have $$(A,B)$$ and you want to find all the even squares between $$A, B$$.

Well, just do it.

If $$0\le A \le n^2 \le B$$ and $$n^2$$ is even, then $$n$$ is even. So let $$n=2m$$. Then $$n^2 = (2m)^2 = 4m$$ so $$0\le A \le 4m^2 \le B$$.

That means $$\frac A4 \le m^2 \le \frac B4$$ and that $$\sqrt{\frac A4} \le m \le \sqrt{\frac B4}$$.

So just find all the integers, $$m$$, between $$\sqrt{\frac A4}$$ and $$\sqrt{\frac B4}$$ and square them and multiply by $$4$$ to get all the even squares.

For example: if you want to find all the even squares between $$101$$ and $$4087$$ we take $$L = \sqrt{\frac {101}4} =\sqrt{25.25}\approx 5.0249$$ and we take $$U = \sqrt{\frac {4087}4}=\sqrt{1021.75} \approx 31.965$$.

Now we just take all the integers between $$5.0249$$ and $$31.965$$, that is, $$6,7,8,9,10,11,........,28, 29, 30, 31$$ and we square them to get $$36, 49, ......, 841,900, 961$$ and we multiply them by $$4$$ to get $$144, 196, ....., 3364, 3600, 3844$$.

And that's that. That is all the even squares between $$101$$ and $$4087$$.