Sunday, November 17, 2024
Google search engine
HomeData Modelling & AIRepeated Unit Divisibility

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




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


Javascript




<script>
 
 
// java script 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
    let rem = 1;
    let power = 1;
    let k = 1;
    while (rem % n != 0)
    {
        k++;
        power = power * 10 % n;
        rem = (rem + power) % n;
    }
     
    return k;
}
 
    // Driver Code
    let n = 13;
    document.write(repUnitValue(n));
     
// This code is contributed by sravan kumar.
 
</script>


Output: 

6

 

Time complexity: O(n)

Auxiliary Space: O(1)

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

RELATED ARTICLES

Most Popular

Recent Comments