Repdigit Numbers
Last Updated :
23 Mar, 2021
Repdigit Number is a number N which has all the digits in its representation in base B equal.
Some of the Repdigit number are:
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 22, 33, 44, 55….
Check if N is an Repdigit number
Given a number N, the task is to check if N is an Repdigit Number in Base B or not. If N is a Repdigit Number in Base B then print “Yes” else print “No”.
Examples:
Input: N = 2000, B = 7
Output: Yes
Explanation:
2000 in base 7 is 5555 which has all digits equal.
Input: N = 112, B = 10
Output: No
Approach:
- One by one find all the digits of N in base B.
- Compare every digit with its previous digit.
- If any digit is not equal to the previous digit then return false.
- Otherwise return true.
Below is the implementation of the above approach:
C++
#include <iostream>
using namespace std;
bool isRepdigit( int num, int b)
{
int prev = -1;
while (num) {
int digit = num % b;
num /= b;
if (prev != -1 && digit != prev)
return false ;
prev = digit;
}
return true ;
}
int main()
{
int num = 2000, base = 7;
isRepdigit(num, base) ? cout << "Yes"
: cout << "No" ;
return 0;
}
|
Java
class GFG{
static boolean isRepdigit( int num, int b)
{
int prev = -1;
while (num != 0)
{
int digit = num % b;
num /= b;
if (prev != -1 && digit != prev)
return false ;
prev = digit;
}
return true ;
}
public static void main(String args[])
{
int num = 2000, base1 = 7;
if (isRepdigit(num, base1))
{
System. out .print( "Yes" );
}
else
{
System. out .print( "No" );
}
}
}
|
Python3
def isRepdigit(num, b) :
prev = - 1
while (num) :
digit = num % b
num / / = b
if (prev ! = - 1 and digit ! = prev) :
return False
prev = digit
return True
num = 2000
base = 7
if (isRepdigit(num, base)):
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
class GFG{
static bool isRepdigit( int num, int b)
{
int prev = -1;
while (num != 0)
{
int digit = num % b;
num /= b;
if (prev != -1 && digit != prev)
return false ;
prev = digit;
}
return true ;
}
public static void Main()
{
int num = 2000, base1 = 7;
if (isRepdigit(num, base1))
{
Console.Write( "Yes" );
}
else
{
Console.Write( "No" );
}
}
}
|
Javascript
<script>
function isRepdigit( num ,b) {
let prev = -1;
while (num != 0) {
let digit = num % b;
num = parseInt(num/b);
if (prev != -1 && digit != prev)
return false ;
prev = digit;
}
return true ;
}
let num = 2000, base1 = 7;
if (isRepdigit(num, base1)) {
document.write( "Yes" );
} else {
document.write( "No" );
}
</script>
|
Time Complexity: O(n)
Reference: http://www.numbersaplenty.com/set/repdigit/
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...