# Difference between sum of even positioned digits and sum of odd positioned digits in a number is equal to 1

A "Good Number" is a number whose difference between the sum of its digits at even places and the sum of digits at odd places is $$1.$$

For example, $$234563$$ is a good number.

Digits at odd location are $$3,5,3$$ (unit place is location 1).
Digits at even location are $$2,4,6$$.
Diff $$=(2+4+6)-(3+5+3)=12-11 = 1$$.

And 123456 is not a good number, because diff=$$(5+3+1)-(2+4+6)=9-12 = -3$$.

Good Numbers from 1 to 100 are $$10,21,32,43,54,65,76,87,98$$ So my question is, in a given range, like 1 to 100 or 1 to 1000 for instance is there a way to find out how many numbers in the range are good numbers without actually having to test each number?

I figured out that all good numbers when divided by 11 would yield a remainder 10. But the converse is not true (if 109 is divided by 11 would yield a remainder 10, but it is not a good number).

I also came across a similar question where given a range we need to find the numbers that on finding the difference between Sum of digits at even location and Sum of digits at odd location would yield a prime number. Since finding difference between the sum of even and odd positioned digits is the base for both the problems it would be of great help if someone could help me with this...

Consider the case of $n$ digits with $n$ even ($n$ odd will be similar). Let $m=\frac n2$. You are interested in how many ways there are to get $m$ numbers in the range $0$ to $9$ to make a given sum. So you can define $N(m,p$) as the number of ways to have $m$ numbers in this range add up to $p$ for the odd places. There is some perturbation for the odd places as you don't allow a leading zero. In this case, $N(2,1)=1, N(2,2)=2, \ldots N(2,9)=9, N(2,10)=9 , \ldots N(2,18)=1$ Then define $M(m,p)$ similarly for the even places, $M(2,0)=1, M(2,1)=2, \ldots M(2,9)=10, M(2,10)=9 , \ldots M(2,18)=1$. Now you can find $\sum N(m,i)M(m,i-1)$ to get the total.
• @Jayanth: The example N and M I gave are for a four digit number. $N(2,9)=9$ says there are $9$ different combinations of numbers that can go in the first and third positions that sum to $9$. These are $18, 27, 36, \ldots$. If the odd positions sum to $9$, the even positions need to sum to $8$. Since $M(2,8)=9$ there are $9\cdot 9=81$ four digit numbers where the odd positions sum to $9$ and the even ones sum to $8$. You need to sum over all the possibilities for the sum of the odds. – Ross Millikan Sep 19 '12 at 16:51