Smallest Integer Divisible by K
We want to find the length of the smallest positive integer ( n ) that is divisible by ( k ), and ( n ) contains only the digit 1.
Here’s the Python code:
|
|
Explanation:
- If ( k ) is even or divisible by 5, there is no solution. This is because any number composed of only the digit 1 will be odd, and its last digit won’t be divisible by 5.
- Start with ( n = 1 ) and keep appending 1 to it until we find a number that is divisible by ( k ).
- Since we only care about the remainder, we can work with ( n \mod k ) to prevent overflow.
- If we’ve looped ( k ) times without finding a solution, there’s no solution.
The time complexity of this code is ( O(k) ), and the space complexity is ( O(1) ).