Suppose we are given a range of integer numbers and are asked to find out the special numbers in the range. A special number is a number that is a positive integer having only 1 digit in its decimal representation. A number that has more than 1 digit in its decimal representation can also be special if the number is divisible by the count of digits in its decimal representation and the quotient value is itself a special number. We return the count of special numbers in the given range (left_limit, right_limit).
So, if the input is like left_limit = 5, right_limit = 30, then the output will be 13.
The special numbers in this range are: 5, 6, 7, 8, 9, 10, 12, 14, 16, 18, 20, 24, and 28.
To solve this, we will follow these steps −
- if right_limit < 10, then
- return right_limit - left_limit + 1
- len_right := size of string representation of (right_limit)
- number_list := [0,1,2,3,4,5,6,7,8,9,10,12,14,16,18]
- for j in range 2 to len_right + 1, do
- for each k in number_list, do
- temp1 := k * j
- if size of string representation of temp1 is same as j, then
- insert temp1 at the end of number_list
- otherwise when len(str(temp1)) > j, then
- come out from the loop
- if number_list[size of number_list - 1] >= right_limit, then
- come out from the loop
- for each k in number_list, do
- delete duplicate values from number_list and sort
- count := 0
- for each temp2 in number_list, do
- if temp2 >= left_limit and temp2 <= right_limit, then
- count := count + 1
- if temp2 >= left_limit and temp2 <= right_limit, then
- return count
Example
Let us see the following implementation to get better understanding −
def strange(left_limit, right_limit): if right_limit < 10: return right_limit - left_limit + 1 len_right = len(str(right_limit)) number_list = [0,1,2,3,4,5,6,7,8,9,10,12,14,16,18] for j in range(2, len_right + 1): for k in number_list: temp1 = k*j if len(str(temp1)) == j: number_list.append(temp1) elif len(str(temp1)) > j: break if number_list[len(number_list)-1] >= right_limit: break number_list = list(set(number_list)) count = 0 for temp2 in number_list: if temp2 >= left_limit and temp2 <= right_limit: count = count + 1 return count print(strange(5, 30))
Input
5, 30
Output
13