Find the least positive integer $n$ such $S(n^2)+7=S(n)$ This is contest math problem:


  For a positive integer $N=\overline{a_{n}a_{n-1}\cdots a_{0}}$ in decimal representation we denote by $S(N)$ the sum of its digits $a_{0}+a_{1}+\cdots+a_{n-1}+a_{n}$. then find the minimal $n$ such
  $$S(n^2)=S(n)-7$$
It is well known that
$$S(n)\equiv n\pmod 9$$

Then, the equation implies: 
$$n^2\equiv n-7\pmod9$$
Note that this implication is not reversible, so we will need to check the solutions we can find.
The latter equation can be written so:
$$n^2-10n+25\equiv 0\pmod 9$$

So $(n-5)^2\equiv 0\pmod 9$. Therefore,
$$n-5\equiv 0\pmod 3$$
So $n\equiv 2\pmod 3$.

On the other hand, note that
$$S(n)=S(n^2)+7\ge 9$$
and since $n\equiv 2\pmod 3$, $S(n)\ge 11$.

Trying a bit yields the solution: $149$.
149
179
389
449
548
749
899

And many others

java code: 

public class PrecalcMS {
public static void main(String[] args) {
    LongStream.range(0, 100000).parallel().filter(x-&gt;digitsum(x)-digitsum(x*x)==7).forEach(x-&gt;System.out.println(x));
}

private static int digitsum(long x) {
    final String pres = ""+x;
    return IntStream.range(0, pres.length()).map(i-&gt;pres.charAt(i)-'0').sum();
}


}