# Repeated Unit Divisibility using C++

In this article, we will discuss finding the number of repeated units divisible by N. Repeated units are the repetitive numbers of 1 only, Let R(k) be the repetitive unit where k is the length of 1’s. E.g R(4) = 1111. So we need to find the minimum number of k for which R(k) is divisible by N, for example −

Input : N = 13
Output : k = 6
Explanation : R(6) i.e 111111 is divisible by 13.

Input : N = 31
Output : k = 15

## Approach to find The Solution

You can approach this problem by checking each value for k starting from 1 where R(k) is divisible by N. But with this solution, we will not find whether N is divisible by any value of R(k). This will make the program too complex and maybe not work too.

An Efficient Approach for the solution of this program is,

• Check whether N is coprime with 10.
• If NOT, then R(k) will not be divisible by N for any value of k.
• If YES, then for each repetitive unit R(1), R(2), R(3)... and so on, Calculate the remainder of division of R(i) and N, So there will be n number of the remainder.
• Find the same remainder values for R(i) and R(j), where R(i) and R(j) are two repeated units so that R(i) - R(j) will be divisible by N.
• aThe difference of R(i) and R(j) will be repeated unit multiplied by some power of 10, But 10 and N are relatively prime, so R(k) will be divisible by N.

## Example

#include <bits/stdc++.h>
using namespace std;

int main() {
int N = 31;
int k = 1;
// checking if N is coprime with 10.
if (N % 2 == 0 || N % 5 == 0){
k = 0;
} else {
int r = 1;
int power = 1;
// check until the remainder is divisible by N.
while (r % N != 0) {
k++;
power = power * 10 % N;
r = (r + power) % N;
}
}
cout << "Value for k : "<< k;
return 0;
}



## Output

Value for k : 15

## Conclusion

In this article, we discuss finding the value of k for R(k), where R(k) is the repeated units divisible by given N.We discussed an optimistic way to find the value of k. We also discussed C++ code to solve this problem. You can write this code in any other language like Java, C, Python, etc. We hope you find this article helpful.

Updated on: 29-Nov-2021

69 Views 