I was trying to solve a question by programming , where one needs to find $X$ such that $X^2\;mod\;M=N$ ,such that $0 \le N \lt M \le 10^6$. Now here I was able to prove:
Let $Y = q * M + X$ where $0 \le X \lt M$ and $1 \le q$. So $M-1 \lt Y$
$Y^2 \;mod\; M = ((Y \;mod\; M) * (Y \;mod\; M)) \;mod\; M = (X * X) \;mod\; M = X^2 \;mod\; M$
But $0 \le X \lt M$
So $0 \le X \lt M$
However few optimisations has been suggested(In the given answer in book) such that :
1.) $\sqrt{N} \le X \lt M$
2.)Further it is also mentioned that the value of $X^2\;mod\;M$ shall repeat from $M/2$ on wards. So we can change the upper bound from $(M-1)$ to $M/2$
How to prove these above two optimsations mathematically?