Find the least positive integer $n$ such $S(n^2)+7=S(n)$

116 Views Asked by At

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$$

2

There are 2 best solutions below

2
On BEST ANSWER

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$.

1
On

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->digitsum(x)-digitsum(x*x)==7).forEach(x->System.out.println(x));
}

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

}