# Number of ones in the smallest repunit

Given a positive integer N whose unit’s digit is 3. Find the number of 1s in the smallest repunit which is divisible by the given number N. Every number whose unit’s digit is 3 has a repunit as its multiple. A repunit is a number which has only ones. It is of the form (10n – 1)/9.

Examples:

Input: 3
Output: 3
As 3 divides 111 which is the smallest repunit
multiple of the number. So the number of ones in 111 is 3.

Input: 13
Output: 6

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

The repunits are 1, 11, 111, 1111, …. the next repunit to x will always be x*10+1. If the remainder left by x repunit is r then remainder left by the next repunit will always be (r*10+1)%n. Since the repunit can be very large, there is no need to find the repunit number. Simply counting the number of ones will give us the answer.
So, find out the remainders of all repunit numbers until the remainder becomes 0. Once it does, then the count of iterations done to make remainder 0 will be the number of 1’s.

Below is the implementation of above approach :

## C++

 `// CPP program to print the number of 1s in ` `// smallest repunit multiple of the number. ` `#include ` `using` `namespace` `std; ` ` `  `// Function to find number of 1s in ` `// smallest repunit multiple of the number ` `int` `countOnes(``int` `n) ` `{ ` `    ``// to store number of 1s in smallest ` `    ``// repunit multiple of the number. ` `    ``int` `count = 1; ` ` `  `    ``// initialize rem with 1 ` `    ``int` `rem = 1; ` ` `  `    ``// run loop until rem becomes zero ` `    ``while` `(rem != 0) { ` ` `  `        ``// rem*10 + 1 here represents ` `        ``// the repunit modulo n ` `        ``rem = (rem * 10 + 1) % n; ` `        ``count++; ` `    ``} ` ` `  `    ``// when remainder becomes 0 ` `    ``// return count ` `    ``return` `count; ` `} ` ` `  `// Driver Code ` `int` `main() ` `{ ` `    ``int` `n = 13; ` ` `  `    ``// Calling function ` `    ``cout << countOnes(n); ` `} `

## Java

 `// Java program to print the  ` `// number of 1s in smallest  ` `// repunit multiple of the number. ` `import` `java.io.*; ` ` `  `class` `GFG  ` `{ ` `// Function to find number  ` `// of 1s in smallest repunit ` `// multiple of the number ` `static` `int` `countOnes(``int` `n) ` `{ ` `    ``// to store number of 1s  ` `    ``// in smallest repunit ` `    ``// multiple of the number. ` `    ``int` `count = ``1``; ` ` `  `    ``// initialize rem with 1 ` `    ``int` `rem = ``1``; ` ` `  `    ``// run loop until  ` `    ``// rem becomes zero ` `    ``while` `(rem != ``0``)  ` `    ``{ ` ` `  `        ``// rem*10 + 1 here  ` `        ``// represents the  ` `        ``// repunit modulo n ` `        ``rem = (rem * ``10` `+ ``1``) % n; ` `        ``count++; ` `    ``} ` ` `  `    ``// when remainder becomes ` `    ``// 0 return count ` `    ``return` `count; ` `} ` ` `  `// Driver Code ` `public` `static` `void` `main (String[] args) ` `{ ` `int` `n = ``13``; ` ` `  `// Calling function ` `System.out.println(countOnes(n)); ` `} ` `} ` ` `  `// This code is contributed by akt_mit `

## Python3

 `# Python3 program to print the  ` `# number of 1s in smallest ` `# repunit multiple of the number. ` ` `  `# Function to find number  ` `# of 1s in smallest repunit ` `# multiple of the number ` `def` `countOnes(n): ` `     `  `    ``# to store number of 1s  ` `    ``# in smallest repunit ` `    ``# multiple of the number. ` `    ``count ``=` `1``; ` ` `  `    ``# initialize rem with 1 ` `    ``rem ``=` `1``; ` ` `  `    ``# run loop until  ` `    ``# rem becomes zero ` `    ``while` `(rem !``=` `0``): ` ` `  `        ``# rem*10 + 1 here represents ` `        ``# the repunit modulo n ` `        ``rem ``=` `(rem ``*` `10` `+` `1``) ``%` `n; ` `        ``count ``=` `count ``+` `1``; ` ` `  `    ``# when remainder becomes ` `    ``# 0 return count ` `    ``return` `count; ` ` `  `# Driver Code ` `n ``=` `13``; ` ` `  `# Calling function ` `print``(countOnes(n)); ` `     `  `# This code is contributed by mits `

## C#

 `// C# program to print the  ` `// number of 1s in smallest  ` `// repunit multiple of the number. ` `using` `System; ` ` `  `class` `GFG ` `{ ` `     `  `// Function to find number  ` `// of 1s in smallest repunit ` `// multiple of the number ` `static` `int` `countOnes(``int` `n) ` `{ ` `    ``// to store number of 1s  ` `    ``// in smallest repunit ` `    ``// multiple of the number. ` `    ``int` `count = 1; ` ` `  `    ``// initialize rem with 1 ` `    ``int` `rem = 1; ` ` `  `    ``// run loop until  ` `    ``// rem becomes zero ` `    ``while` `(rem != 0)  ` `    ``{ ` ` `  `        ``// rem*10 + 1 here represents ` `        ``// the repunit modulo n ` `        ``rem = (rem * 10 + 1) % n; ` `        ``count++; ` `    ``} ` ` `  `    ``// when remainder becomes 0 ` `    ``// return count ` `    ``return` `count; ` `} ` ` `  `// Driver Code ` `static` `public` `void` `Main () ` `{ ` `int` `n = 13; ` ` `  `// Calling function ` `Console.WriteLine (countOnes(n)); ` `} ` `} ` ` `  `// This code is contributed by ajit `

## PHP

 ` `

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.

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 : jit_t, Mithun Kumar

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.