# Repunit numbers

A number is a Repunit in base B if it can be represented as a string of three or more 1’s in a base >= 2.

### Check if N is a Repunit number

Given an integer N, the task is to check if N is a Repunit number in base B.

Examples:

Input: N = 31, B = 5
Output: Yes
31 can be written as 111 base in 5

Input: N = 5, B = 2
Output: No
5 is 101 in base 2

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

Approach: We will count the number of one’s in the base B of a given number N and also count the number of digits in the base B of a given number N. If they are same, print “YES” else print “NO”.

For Example:

N = 31, B = 5
31 can be written as 111 base in 5, So number of one’s in base B of a given number N = 3 and number of digits in the base B of a given number N = 3
Since both are equal hence 31 is a Repunit number in base 5.

Below is the implementation of the above approach:

## C++

 `// C++ implementation  to check ` `// if a number is Repunit Number ` ` `  `#include ` `#include ` `using` `namespace` `std; ` ` `  `// Function to check if a number ` `// contains all the digits 0, 1, .., (b-1) ` `// an equal number of times ` `bool` `isRepunitNum(``int` `n, ``int` `b) ` `{ ` `    ``// to store number of digits of n ` `    ``// in base B ` `    ``int` `length = 0; ` `    ``// to count frequency of digit 1 ` `    ``int` `countOne = 0; ` `    ``while` `(n != 0) { ` `        ``int` `r = n % b; ` `        ``length++; ` `        ``if` `(r == 1) ` `            ``countOne++; ` `        ``n = n / b; ` `    ``} ` ` `  `    ``// condition to check three or more 1's ` `    ``// and number of ones is equal to number ` `    ``// of digits of n in base B ` `    ``return` `countOne >= 3 && countOne == length; ` `} ` ` `  `// Driver Code ` `int` `main() ` `{ ` `    ``// taking inputs ` `    ``int` `n = 31; ` `    ``int` `base = 2; ` ` `  `    ``// function to check ` `    ``if` `(isRepunitNum(n, base)) ` `        ``cout << ``"Yes"``; ` `    ``else` `        ``cout << ``"NO"``; ` `    ``return` `0; ` `} `

## Java

 `// Java implementation to check ` `// if a number is Repunit Number ` `class` `GFG{ ` ` `  `// Function to check if a number ` `// contains all the digits 0, 1, .., (b-1) ` `// an equal number of times ` `static` `boolean` `isRepunitNum(``int` `n, ``int` `b) ` `{ ` `    ``// to store number of digits of n ` `    ``// in base B ` `    ``int` `length = ``0``; ` `     `  `    ``// to count frequency of digit 1 ` `    ``int` `countOne = ``0``; ` `    ``while` `(n != ``0``)  ` `    ``{ ` `        ``int` `r = n % b; ` `        ``length++; ` `        ``if` `(r == ``1``) ` `            ``countOne++; ` `        ``n = n / b; ` `    ``} ` ` `  `    ``// condition to check three or more 1's ` `    ``// and number of ones is equal to number ` `    ``// of digits of n in base B ` `    ``return` `countOne >= ``3` `&&  ` `           ``countOne == length; ` `} ` ` `  `// Driver Code ` `public` `static` `void` `main(String[] args)  ` `{ ` `    ``// taking inputs ` `    ``int` `n = ``31``; ` `    ``int` `base = ``2``; ` ` `  `    ``// function to check ` `    ``if` `(isRepunitNum(n, base)) ` `        ``System.out.print(``"Yes"``);  ` `    ``else` `        ``System.out.print(``"No"``); ` `} ` `} ` ` `  `// This code is contributed by rock_cool `

## Python3

 `# Python3 implementation to check ` `# if a number is Repunit Number ` ` `  `# Function to check if a number ` `# contains all the digits 0, 1, .., (b-1) ` `# an equal number of times ` `def` `isRepunitNum(n, b): ` ` `  `    ``# to store number of digits of n ` `    ``# in base B ` `    ``length ``=` `0``; ` ` `  `    ``# to count frequency of digit 1 ` `    ``countOne ``=` `0``; ` `    ``while` `(n !``=` `0``): ` `        ``r ``=` `n ``%` `b; ` `        ``length ``+``=` `1``; ` `        ``if` `(r ``=``=` `1``): ` `            ``countOne ``+``=` `1``; ` `        ``n ``=` `n ``/``/` `b; ` ` `  `    ``# condition to check three or more 1's ` `    ``# and number of ones is equal to number ` `    ``# of digits of n in base B ` `    ``return` `countOne >``=` `3` `and` `countOne ``=``=` `length; ` ` `  `# Driver Code ` `if` `__name__ ``=``=` `'__main__'``: ` `     `  `    ``# taking inputs ` `    ``n ``=` `31``; ` `    ``base ``=` `2``; ` ` `  `    ``# function to check ` `    ``if` `(isRepunitNum(n, base)): ` `        ``print``(``"Yes"``); ` `    ``else``: ` `        ``print``(``"No"``); ` ` `  `# This code is contributed by 29AjayKumar `

## C#

 `// C# implementation to check ` `// if a number is Repunit Number ` `using` `System; ` `class` `GFG{ ` ` `  `// Function to check if a number ` `// contains all the digits 0, 1, .., (b-1) ` `// an equal number of times ` `static` `bool` `isRepunitNum(``int` `n, ``int` `b) ` `{ ` `    ``// to store number of digits of n ` `    ``// in base B ` `    ``int` `length = 0; ` `     `  `    ``// to count frequency of digit 1 ` `    ``int` `countOne = 0; ` `    ``while` `(n != 0)  ` `    ``{ ` `        ``int` `r = n % b; ` `        ``length++; ` `        ``if` `(r == 1) ` `            ``countOne++; ` `        ``n = n / b; ` `    ``} ` ` `  `    ``// condition to check three or more 1's ` `    ``// and number of ones is equal to number ` `    ``// of digits of n in base B ` `    ``return` `countOne >= 3 &&  ` `           ``countOne == length; ` `} ` ` `  `// Driver Code ` `public` `static` `void` `Main()  ` `{ ` `    ``// taking inputs ` `    ``int` `n = 31; ` `    ``int` `base1 = 2; ` ` `  `    ``// function to check ` `    ``if` `(isRepunitNum(n, base1)) ` `        ``Console.Write(``"Yes"``);  ` `    ``else` `        ``Console.Write(``"No"``); ` `} ` `} ` ` `  `// This code is contributed by Code_Mech `

Output:

```Yes
```

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.

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.