Number of Ones in the Smallest repunit


In this problem, we simply need to print the number of ones in the smallest repunit.

A repunit is a positive number like 11, 111, or 1111 in recreational mathematics that only has the digit 1. A repunit is of the form $\mathrm{(10*n-1)/9}$

Example

$\mathrm{(10*10-1)/9}$ gives 11.

$\mathrm{(10*100-1)/9}$ gives 111.

$\mathrm{(10*1000-1)/9}$ gives 1111.

The above problem states that we are given any positive integer N with its unit digit 3 and we need to determine the smallest repunit that is divisible by the given number N.

For example,

If we are given N=13.

Output: 6

N i.e. 13 perfectly divides 111111 to give 8547.

111111 is the smallest repunit which is divisible by 13. So the number of ones in the smallest repunit is 6 giving the desired output.

Algorithm

Since we know that repunits are 1, 11, 111, 1111, and so on. The subsequent repunit after x can be defined as $\mathrm{(x*10+1)}$.

This algorithm simply works on the concept that the repunits remainder will always be $\mathrm{(rem*10+1)\%N}$ if the remainder left by integer N be rem.

Determining the repunit number can be too hectic as the number can be quite large thus we will find the answer by updating the remainder until it becomes 0 and counting the number of ones by updating at every step. The number of iterations it takes to make the remainder 0 will be the number of ones in the smallest repunit.

Below is the step-by-step description of the algorithm −

  • Step 1 − Declare variable remainder as 1 to store the remainder left by N at every iteration and itr as 1 to calculate the number of iterations.

  • Step 2 − Use while loop until remainder becomes 0.

  • Step 3 − At every step, update the remainder and increase itr by 1.

  • Step 4 − Once the remainder equals 0, return itr.

Let’s try this approach for N=13.

Since we declare remainder and itr as 1 before the while loop.

Now,

  • At iteration 1, remainder will be (remainder*10+1)%N which gives 11. remainder=11 and itr=2. Follow the same algorithm until the remainder becomes 0.

  • At iteration 2, remainder=7 and itr=3

  • At iteration 3, remainder=6 and itr=4

  • At iteration 4, remainder=9 and itr=5

  • At iteration 5, remainder=0 and itr=6.

Since remainder becomes 0, we will return itr i.e. 6 which is the desired output.

Approach

Below is the implementation of above approach in C++ −

#include <iostream>
#include<bits/stdc++.h>

using namespace std;

//function to calculate no of ones in smallest repunit
int numberOfones(int N){  
   int remainder=1;
   
   int itr=1; // to store no of iterations
   
   while(remainder!=0){
      //update remainder
      remainder=(remainder*10 + 1)% N;
   
      itr++; //increase itr by 1 to get number of 1's in repunit
   }
   
   return itr;
}
int main(){
   int N=23;
   cout<<numberOfones(N);
   return 0;
}

Output

22

The smallest repunit number divisible by 23 will consist of 22 1’s in it.

Conclusion

In the above article, we have tried to solve the problem to get the number of ones in the smallest repunit divisible by any positive integer N whose unit digit is 3. I hope this article helps you to clear your concept about the problem.

Updated on: 14-Mar-2023

204 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements