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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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));
    }
}

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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.

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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.

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?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
?>

chevron_right


Output:

6


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.



Improved By : Sam007, jit_t



Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.