Program to find out the number of special numbers in a given range in Python

A special number is a positive integer that either has only 1 digit, or if it has multiple digits, it must be divisible by its digit count and the quotient must also be a special number. Let's find how many special numbers exist within a given range.

Understanding Special Numbers

The definition of special numbers works recursively:

  • Single-digit numbers (1-9) are always special
  • Multi-digit numbers are special if: number ÷ digit_count = special_number
  • For example: 12 ÷ 2 = 6 (6 is special, so 12 is special)

Example

For the range [5, 30], the special numbers are: 5, 6, 7, 8, 9, 10, 12, 14, 16, 18, 20, 24, and 28, giving us a total count of 13.

Algorithm Steps

  • If right_limit < 10, return right_limit - left_limit + 1
  • Generate all possible special numbers up to the right limit
  • Start with single-digit special numbers [0-9] and known 2-digit specials
  • For each digit length, multiply existing special numbers by the digit count
  • Keep only valid results that match the expected digit length
  • Count special numbers within the given range

Implementation

def count_special_numbers(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
    
    # Remove duplicates and sort
    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

# Test the function
print(count_special_numbers(5, 30))
13

How It Works

The algorithm builds special numbers iteratively:

  1. Start with known special numbers up to 2 digits
  2. For 3-digit numbers: multiply 2-digit specials by 3
  3. For 4-digit numbers: multiply 3-digit specials by 4, and so on
  4. Filter results to match the expected digit length
  5. Count numbers within the specified range

Verification Example

def is_special(n):
    if n < 10:
        return n > 0  # Single digits 1-9 are special
    
    digit_count = len(str(n))
    if n % digit_count != 0:
        return False
    
    quotient = n // digit_count
    return is_special(quotient)

# Verify some numbers from our range
test_numbers = [12, 14, 16, 18, 20, 24, 28]
for num in test_numbers:
    print(f"{num} is special: {is_special(num)}")
12 is special: True
14 is special: True
16 is special: True
18 is special: True
20 is special: True
24 is special: True
28 is special: True

Conclusion

This algorithm efficiently generates special numbers by building them iteratively from smaller special numbers. The approach avoids checking every number in the range by pre-generating all possible special numbers up to the limit.

---
Updated on: 2026-03-26T17:49:42+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements