Repunit numbers - GeeksforGeeks

16 Jul, 2021
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. 
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 5Input: 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++
Java
Python3
C#
Javascript
// 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 timesbool 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 Codeint main(){    // taking inputs    int n = 31;    int base = 2;     // function to check    if (isRepunitNum(n, base))        cout << "Yes";    else        cout << "NO";    return 0;}
// Java implementation to check// if a number is Repunit Numberclass GFG{ // Function to check if a number// contains all the digits 0, 1, .., (b-1)// an equal number of timesstatic 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 Codepublic 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 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 timesdef 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 Codeif __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# implementation to check// if a number is Repunit Numberusing System;class GFG{ // Function to check if a number// contains all the digits 0, 1, .., (b-1)// an equal number of timesstatic 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 Codepublic 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
<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>
Yes
 
Time Complexity: O(logbn)
Auxiliary Space: O(1)
Reference: http://www.numbersaplenty.com/set/repunit/ 
rock_cool
Code_Mech
29AjayKumar
GauravRajput1
souravmahato348
series
Mathematical
Mathematical
series
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Program to print prime numbers from 1 to N.
Segment Tree | Set 1 (Sum of given range)
Modular multiplicative inverse
Count all possible paths from top left to bottom right of a mXn matrix
Fizz Buzz Implementation
Check if a number is Palindrome
Program to multiply two matrices
Count ways to reach the n'th stair
Merge two sorted arrays with O(1) extra space
Generate all permutation of a set in Python