Repeated Unit Divisibility
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 <bits/stdc++.h> 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 = 13print(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
<?php // PHP program to find least value of // k for which R(k) is divisible by n function repUnitValue($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 $rem = 1; $power = 1; $k = 1; while ($rem % $n != 0) { $k++; $power = $power * 10 % $n; $rem = ($rem + $power) % $n; } return $k; } // Driver Code $n = 13; echo repUnitValue($n); // This code is contributed by aj_36 ?> |
6
Recommended Posts:
- Check divisibility by 7
- Sub-string Divisibility by 11 Queries
- Sub-string Divisibility by 3 Queries
- Divisibility by 12 for a large number
- To check divisibility of any large number by 999
- Check the divisibility of Hexadecimal numbers
- Check divisibility in a binary stream
- Divisibility by 64 with removal of bits allowed
- Check if a large number is divisibility by 15
- Maximize profit when divisibility by two numbers have associated profits
- Divisibility by 3 where each digit is the sum of all prefix digits modulo 10
- Maximum path sum for each position with jumps under divisibility condition
- Sum of integers upto N with given unit digit (Set 2)
- Sum of integers upto N with given unit digit
- Repeated sum of first N natural numbers
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

