
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Smallest Integer Divisible by K in Python
Suppose we have a positive integer K, we need find the smallest positive integer N such that N is divisible by K, and N only contains the digit 1. We have to find the length of N. If there is no such N, return -1. So if the input is like 3, then the output will be 3. The smallest answer will be N = 111.
To solve this, we will follow these steps −
- if k is even, or k is divisible by 5, then return -1
- set r := 0 and N = 1
- for i in range 1 to K + 1
- r := (r * 10 + 1) mod k
- if r = 0, then return i
Let us see the following implementation to get better understanding −
Example
class Solution(object): def smallestRepunitDivByK(self, K): if K%2==0 or K%5 ==0: return -1 r = 0 N=1 for i in range(1,K+1): r = (r*10 + 1)%K if r == 0: return i ob = Solution() print(ob.smallestRepunitDivByK(11))
Input
11
Output
2
- Related Articles
- Python Program for Smallest K digit number divisible by X
- C++ Programming for Smallest K digit number divisible by X?
- C++ Program for Smallest K digit number divisible by X?
- Java Program for Smallest K digit number divisible by X
- Program to find length of smallest sublist that can be deleted to make sum divisible by k in Python
- Subarray Sums Divisible by K in C++
- Find smallest element greater than K in Python
- Largest K digit number divisible by X in C++
- Smallest number that is divisible by first n numbers in JavaScript
- Program to find number of consecutive subsequences whose sum is divisible by k in Python
- Program to find smallest value of K for K-Similar Strings in Python
- Count subarrays whose product is divisible by k in C++
- Find nth number that contains the digit k or divisible by k in C++
- Program to check if array pairs are divisible by k or not using Python
- Count all sub-arrays having sum divisible by k

Advertisements