Open In App

Repunit numbers

Improve
Improve
Like Article
Like
Save
Share
Report

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


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


Javascript




<script>
 
// Javascript 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
    function isRepunitNum( n ,b) {
        // to store number of digits of n
        // in base B
        let length = 0;
 
        // to count frequency of digit 1
        let countOne = 0;
        while (n != 0) {
            let r = n % b;
            length++;
            if (r == 1)
                countOne++;
            n = parseInt(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
      
        // taking inputs
        let n = 31;
        let base = 2;
 
        // function to check
        if (isRepunitNum(n, base))
            document.write("Yes");
        else
            document.write("No");
             
// This code contributed by gauravrajput1
 
</script>


Output: 

Yes

 

Time Complexity: O(logbn)

Auxiliary Space: O(1)

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



Last Updated : 16 Jul, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads