Count of x in interval [l; r]

30 Views Asked by At

I have $l$ and $r$ and I have to find the number of such integers $x$ $(l ≤ x ≤ r)$, that the first digit of integer $x$ equals the last one (in decimal notation). For example, such numbers as $101$, $477474$ or $9$ will be included in the answer and $47$, $253$ or $1020$ will not.

Examples:

#$1:$

$l = 2$ and $r = 47$

$answer = 12$

#$2:$

$l = 47$ and $r = 1024$

$answer = 98$

Sorry for my english. if I do any wrong , sorry and please edit it.

I find answer so:

if($first$ $number$ $of$ $n$ $>$ $last$ $number$ $of$ $n$) $k = 1$ else $k = 0$

$answer = n/10 + 9 - k$

answer = (answer for $r$) - (answer for ($l-1$))

I want to know proof of this formula

I saw these:

len -> length of n

| len | count of numbers that are x |

$| 1 | 9 |$

$| 2 | 9 |$

$| 3 | 90 |$

$| 4 | 900 |$

...

| $k$ |$9000...00$ (count of zeros is $k-2$)|

explanation of the above:

$len = 1:$

$1, 2, 3, 4, 5, 6, 7, 8, 9$

$ len = 2: $

$11, 22, 33, 44, 55, 66, 77, 88, 99$

...

answer is sum of all numbers(for len) to $n$. $->$ $9 + 9 + 90 + 900 + 9000 ...$

This is geometric sequence.