Let $x$ be the year that is $P$ years before $2017$. If the sum of the digits of $x$ is equal to $P$, how many such $P$ exist? I have this question that I found in a past paper. I’ve solved it, but would there be a more efficient way to solve this? Here is the question:

Let $x$ be the year that is $P$ years before $2017$. If the sum of the digits of $x$ is equal to $P$, how many such $P$ exist?

I have derived a few things, most of them pretty obvious:

*

*$P$ and $x$ are integers.

*$P$ must be less than or equal to 28, because the year with the greatest sum of digits is 1999, with a digit sum of 28

*Because of 2., $x$ has a range of 1 to 28.

*$x$ can only be a 4-digit number.

Now, all I did was to start from 1 and go all the way back to 28 years, substituting values for $x$ and $P$. I feel like this is very inefficient. Is there a better way to do this?
 A: Let $$x=1000a_1+100a_2+10a_3+a_4$$
$$x=2017-P$$
$$1000a_1+100a_2+10a_3+a_4=2017-(a_1+a_2+a_3+a_4)$$
$$1001a_1+101a_2+11a_3+2a_4=2017$$
Since $a_1, a_2, a_3, a_4\in \{0,1,2,3,4,5,6,7,8,9\}$, we conclude that $a_1\leq2$.
Taking $a_1=2$, means that $101a_2+11a_3+2a_4=15$. This further gives $a_2=0$ so that $11a_3+2a_4=15$.
Now, $11a_3+2a_4=15$ has only one possible solution $a_3=1, a_4=2$.
Taking $a_1=1$, means that $101a_2+11a_3+2a_4=1016$. This further gives $a_2=9$ (otherwise $11a_3+2a_4>117$) so that $11a_3+2a_4=107$.
Now, $11a_3+2a_4=107$ has only one possible solution $a_3=9, a_4=4$. Note that if $a_3<9$, we must have $a_4>18$ which is not possible.
Thus, $x\in\{2012,1994\}$ and $P\in\{5,23\}$.
Consequently, there are only $2$ possible values of $P$.
A: You've identified the possible years as being in the form $19ab$ or $20ab$.
If the year is $19ab$ then that has a value of $1900 + 10a +b$.
You can create two equations:
First $P=2017-(1900+10a+b) \Rightarrow P=117-10a-b$
Then $P=1 + 9 + a + b \Rightarrow P=10+a+b$
Combining those gives $10+a+b=117-10a-b$
$2b=107-11a$
$b=\frac{107-11a}2$
For this to work, $a$ must be odd.
$a=9 \Rightarrow b=4$
$a=7 \Rightarrow b=15$ - too large
$a=5, 3, 1$ will fail similarly.
$a=9 \Rightarrow b=4$
If the year is $20ab$ then that has a value of $2000 + 10a +b$.
You can create two equations:
First $P=2017-(2000+10a+b) \Rightarrow P=17-10a-b$
Then $P=2 + 0 + a + b \Rightarrow P=2+a+b$
Combining those gives $2+a+b=17-10a-b$
$2b=15-11a$
$b=\frac{15-11a}2$
For this to work, $a$ must be odd.
$a=1 \Rightarrow b=2$
$a=3 \Rightarrow b<0$ , similarly for $a=5, 7, 9$.
That gives you two possible answers: 1994 and 2012
