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

**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 baseBof a given numberN= 3 and number of digits in the baseBof a given numberN= 3

Since both are equal hence 31 is a Repunit number in base5.

Below is the implementation of the above approach:

## C++

`// C++ implementation to check ` `// if a number is Repunit Number ` ` ` `#include <iostream> ` `#include <math.h> ` `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; ` `} ` |

*chevron_right*

*filter_none*

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

*chevron_right*

*filter_none*

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

*chevron_right*

*filter_none*

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

*chevron_right*

*filter_none*

**Output:**

Yes

**Reference**: http://www.numbersaplenty.com/set/repunit/

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.

## Recommended Posts:

- Number of ones in the smallest repunit
- Print numbers such that no two consecutive numbers are co-prime and every three consecutive numbers are co-prime
- Count numbers which can be constructed using two numbers
- Maximum sum of distinct numbers such that LCM of these numbers is N
- Numbers less than N which are product of exactly two distinct prime numbers
- Print N lines of 4 numbers such that every pair among 4 numbers has a GCD K
- Absolute Difference between the Sum of Non-Prime numbers and Prime numbers of an Array
- Absolute difference between the Product of Non-Prime numbers and Prime numbers of an Array
- Count numbers which are divisible by all the numbers from 2 to 10
- Fill the missing numbers in the array of N natural numbers such that arr[i] not equal to i
- Check if a given pair of Numbers are Betrothed numbers or not
- Number of ways to obtain each numbers in range [1, b+c] by adding any two numbers in range [a, b] and [b, c]
- Count of numbers upto M divisible by given Prime Numbers
- Maximize count of equal numbers in Array of numbers upto N by replacing pairs with their sum
- Count prime numbers that can be expressed as sum of consecutive prime numbers
- Count of N-digit Numbers having Sum of even and odd positioned digits divisible by given numbers
- How to swap two numbers without using a temporary variable?
- Lucky Numbers
- Ugly Numbers
- Write a program to add two numbers in base 14

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.