Skip to content
Discussion options

You must be logged in to vote

We need to find the smallest number consisting only of digit '1' that is divisible by k.

Approach:

  • I can't actually build the number because it might be extremely large
  • Instead, I can work with remainders and build the number digit by digit
  • The number n can be represented as: n = 1, 11, 111, 1111, etc.
  • Each new number is the previous number × 10 + 1

Let's implement this solution in PHP: 1015. Smallest Integer Divisible by K

<?php
/**
 * @param Integer $k
 * @return Integer
 */
function smallestRepunitDivByK($k) {
    // If k is even or divisible by 5, no solution exists
    // because numbers ending with 1 can't be divisible by 2 or 5
    if ($k % 2 == 0 || $k % 5 == 0) {
        return -1

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@basharul-siddike
Comment options

@mah-shamim
Comment options

mah-shamim Nov 25, 2025
Maintainer Author

Answer selected by basharul-siddike
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
question Further information is requested medium Difficulty
2 participants