Program to find multiple of n with only two digits in Python

Suppose we have a number n. We have to find the least positive value x such that x is made up of only two digits 9's and 0's, and x is multiple of n.

So, if the input is like n = 26, then the output will be 90090.

Algorithm

To solve this, we will follow these steps ?

  • m := 9
  • x := 1
  • while m is not divisible by n, do
    • x := x + 1
    • m := replace all 1s with 9s in the binary form of x
  • return m as integer

How It Works

The algorithm uses binary representation where each bit position represents whether to use 9 (for bit 1) or 0 (for bit 0). Starting with x=1 (binary "1"), we replace '1' with '9' to get 9. If not divisible, we increment x to get different combinations of 9s and 0s.

Example

Let us see the following implementation to get better understanding ?

def solve(n):
    m = 9
    x = 1
    while m % n != 0:
        x += 1
        m = int(bin(x)[2:].replace('1','9'))
    return m

n = 26
print(solve(n))

The output of the above code is ?

90090

Step-by-Step Execution

Let's trace through the algorithm for n = 26 ?

def solve_with_trace(n):
    m = 9
    x = 1
    print(f"Initial: x={x}, binary={bin(x)[2:]}, m={m}, m%{n}={m%n}")
    
    while m % n != 0:
        x += 1
        binary_str = bin(x)[2:]
        m = int(binary_str.replace('1','9'))
        print(f"x={x}, binary={binary_str}, m={m}, m%{n}={m%n}")
    
    return m

result = solve_with_trace(26)
print(f"Final result: {result}")
Initial: x=1, binary=1, m=9, m%26=9
x=2, binary=10, m=90, m%26=12
x=3, binary=11, m=99, m%26=21
x=4, binary=100, m=900, m%26=16
x=5, binary=101, m=909, m%26=25
x=6, binary=110, m=990, m%26=14
x=7, binary=111, m=999, m%26=23
x=8, binary=1000, m=9000, m%26=24
x=9, binary=1001, m=9009, m%26=7
x=10, binary=1010, m=9090, m%26=22
x=11, binary=1011, m=9099, m%26=21
x=12, binary=1100, m=9900, m%26=2
x=13, binary=1101, m=9909, m%26=1
x=14, binary=1110, m=9990, m%26=0
Final result: 90090

Alternative Implementation

Here's a more explicit version that shows the pattern clearly ?

def find_multiple_with_9_and_0(n):
    """Find smallest positive number containing only 9s and 0s that's divisible by n"""
    candidates = [9]  # Start with single digit 9
    
    while candidates:
        current = candidates.pop(0)
        if current % n == 0:
            return current
        
        # Generate next candidates by appending 0 or 9
        candidates.append(current * 10)      # append 0
        candidates.append(current * 10 + 9)  # append 9
    
    return None

# Test with different values
test_cases = [26, 7, 13]
for n in test_cases:
    result = find_multiple_with_9_and_0(n)
    print(f"n = {n}: {result}")
n = 26: 90090
n = 7: 999
n = 13: 90909

Conclusion

The binary representation approach efficiently generates all possible combinations of 9s and 0s by treating each bit as a digit selector. This method guarantees finding the smallest such multiple for any given number n.

Updated on: 2026-03-26T15:11:38+05:30

307 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements