Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Python Program for Smallest K digit number divisible by X
In this article, we will learn how to find the smallest K-digit number that is divisible by a given number X. This is a common mathematical programming problem that involves understanding number ranges and divisibility concepts.
Problem Statement
Given two integers K and X, we need to find the smallest K-digit number that is divisible by X.
For example:
- K=3, X=7: Find smallest 3-digit number divisible by 7
- K=4, X=13: Find smallest 4-digit number divisible by 13
Approach
The solution follows these steps:
- Calculate MIN: The smallest K-digit number is 10^(K-1). For K=3, MIN = 100; for K=4, MIN = 1000.
- Check divisibility: If MIN is divisible by X, then MIN is our answer.
- Find next divisible number: If not, find the next number after MIN that is divisible by X using the formula: MIN + (X - MIN % X).
Implementation
def smallest_k_digit_divisible(k, x):
# Calculate smallest k-digit number
min_num = pow(10, k - 1)
# If already divisible by x
if min_num % x == 0:
return min_num
else:
# Find next number divisible by x
return min_num + (x - min_num % x)
# Test cases
k = 3
x = 7
result = smallest_k_digit_divisible(k, x)
print(f"Smallest {k}-digit number divisible by {x}: {result}")
k = 4
x = 13
result = smallest_k_digit_divisible(k, x)
print(f"Smallest {k}-digit number divisible by {x}: {result}")
Smallest 3-digit number divisible by 7: 105 Smallest 4-digit number divisible by 13: 1001
How It Works
Let's trace through an example where K=3 and X=7:
k, x = 3, 7
# Step 1: Find smallest 3-digit number
min_num = pow(10, k - 1) # 10^(3-1) = 100
print(f"Smallest {k}-digit number: {min_num}")
# Step 2: Check if divisible by x
remainder = min_num % x
print(f"{min_num} % {x} = {remainder}")
if remainder == 0:
result = min_num
else:
# Step 3: Find next divisible number
result = min_num + (x - remainder)
print(f"Next divisible number: {min_num} + ({x} - {remainder}) = {result}")
print(f"Answer: {result}")
Smallest 3-digit number: 100 100 % 7 = 2 Next divisible number: 100 + (7 - 2) = 105 Answer: 105
Edge Cases
The algorithm handles various edge cases correctly:
# Edge case 1: When MIN is already divisible
print("K=2, X=5:")
result = smallest_k_digit_divisible(2, 5)
print(f"Result: {result}") # 10 is divisible by 5
# Edge case 2: Large divisor
print("\nK=3, X=999:")
result = smallest_k_digit_divisible(3, 999)
print(f"Result: {result}")
# Edge case 3: X = 1 (every number is divisible by 1)
print("\nK=4, X=1:")
result = smallest_k_digit_divisible(4, 1)
print(f"Result: {result}")
K=2, X=5: Result: 10 K=3, X=999: Result: 999 K=4, X=1: Result: 1000
Conclusion
This algorithm efficiently finds the smallest K-digit number divisible by X using mathematical properties of divisibility. The time complexity is O(1) as it uses direct mathematical calculations rather than iterating through numbers.
