Related Articles

# Repeated Unit Divisibility

• Last Updated : 07 Apr, 2021

A number which consists entirely repeated number of times one is said to be Repeated Unit. We shall define R(k) of length k repeated unit number. For example, R(6) = 111111.
For a given number n, a positive integer and GCD(n, 10) = 1, there exists a value k such that R(k) is divisible by n. Now let A(n) be the least such value of k i.e. A(n) = k.
Thus, we have to find for the given value of n the least such value of A(n) for which k times repeated one is divided by n.
Examples:

```Input : n = 7
Output : A(7) = 6
A(7) = 6 means 6 times one i.e. (111111) is divided by 7.

Input : n = 41
Output : A(41) = 5
A(41) = 5 means 5 times one i.e. (11111) is divided by 41.```

First of all if the given number should be coprime with 10 otherwise return 0.
If the given number is coprime with 10 then we have to find smallest number k such that R(k) = 0 mod n
Consider the repeated units R(1), R(2), R(3), R(4) and so on. For each repeated unit R(j) suppose when calculating remainder of R(j) divided by n. There are n conceivable remainders. We get that R(i), R(j) where i < j have same remainder when divided by n. It follows that R(j) – R(i) is divided by n. This difference is repeated unit multiplied by power of 10. But, as we know that 10 and n are relatively prime, n divides R(k).

## C++

 `// CPP program to find least value of``// k for which R(k) is divisible by n``#include ``using` `namespace` `std;` `// To find least value of k``int` `repUnitValue(``int` `n)``{` `    ``// To check n is coprime or not``    ``if` `(n % 2 == 0 || n % 5 == 0)``        ``return` `0;` `    ``// to store R(k) mod n and 10^k``    ``// mod n value``    ``int` `rem = 1;``    ``int` `power = 1;``    ``int` `k = 1;``    ``while` `(rem % n != 0)``    ``{``        ``k++;``        ``power = power * 10 % n;``        ``rem = (rem + power) % n;``    ``}``    ` `    ``return` `k;``}` `// Driver code``int` `main()``{``    ``int` `n = 13;``    ``cout << repUnitValue(n);``    ``return` `0;``}`

## Java

 `// Java program to find least value of``// k for which R(k) is divisible by n``import` `java.util.*;` `class` `GFG {` `    ``// To find least value of k``    ``public` `static` `int` `repUnitValue(``int` `n)``    ``{``        ` `        ``// To check n is coprime or not``        ``if` `(n % ``2` `== ``0` `|| n % ``5` `== ``0``)``            ``return` `0``;` `        ``// to store the R(k) mod n and``        ``// 10^k mod n value``        ``int` `rem = ``1``;``        ``int` `power = ``1``;``        ``int` `k = ``1``;``        ``while` `(rem % n != ``0``)``        ``{``            ``k++;``            ``power = power * ``10` `% n;``            ``rem = (rem + power) % n;``        ``}``        ` `        ``return` `k;``    ``}` `    ``// Driver code``    ``public` `static` `void` `main(String[] args)``    ``{``        ``int` `n = ``13``;``        ``System.out.println(repUnitValue(n));``    ``}``}`

## Python3

 `# python program to find least value of``# k for which R(k) is divisible by n` `# To find least value of k``def` `repUnitValue(n):``    ` `    ``# To check n is coprime or not``    ``if` `(n ``%` `2` `=``=` `0` `or` `n ``%` `5` `=``=` `0``):``        ``return` `0` `    ``# to store R(k) mod n and 10^k``    ``# mod n value``    ``rem ``=` `1``    ``power ``=` `1``    ``k ``=` `1``    ``while` `(rem ``%` `n !``=` `0``):``        ` `        ``k ``+``=` `1``        ``power ``=` `power ``*` `10` `%` `n``        ``rem ``=` `(rem ``+` `power) ``%` `n``    ` `    ``return` `k``    ` `# Driver code``n ``=` `13``print``(repUnitValue(n))` `# This code is contributed by Sam007.`

## C#

 `// C# program to find least value of``// k for which R(k) is divisible by n``using` `System;` `public` `class` `GFG {``    ` `    ``// To find least value of k``    ``public` `static` `int` `repUnitValue(``int` `n)``    ``{``        ` `        ``// To check n is coprime or not``        ``if` `(n % 2 == 0 || n % 5 == 0)``            ``return` `0;` `        ``// to store the R(k) mod n and``        ``// 10^k mod n value``        ``int` `rem = 1;``        ``int` `power = 1;``        ``int` `k = 1;``        ``while` `(rem % n != 0)``        ``{``            ``k++;``            ``power = power * 10 % n;``            ``rem = (rem + power) % n;``        ``}``        ` `        ``return` `k;``    ``}` `    ``public` `static` `void` `Main()``    ``{``        ``int` `n = 13;``        ``Console.Write(repUnitValue(n));``    ``}``}` `// This code is contributed by Sam007.`

## PHP

 ``

## Javascript

 ``
Output:
`6`

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

My Personal Notes arrow_drop_up