Repdigit Numbers in Java
A combination of repeated and digit, the term is. A repdigit, also known as a monodigit or a positional number system, is a natural number in recreational mathematics made up of repeated occurrences of the same digit (often implicitly decimal). These are a few examples, 99, 555, 1111, and 88888. Palindromic and multiples of repunits and repdigits are all numbers. In particular, the Mersenne primes and the repunit primes are two other well-known types of repdigits (which are repdigits when represented in the binary).
Repdigits are a representation of the number a B b 1 B 1 in base Y display style Y, where 0 x Y display style 0 x Y is the replicated digit and 1 y display style 1 y is the count of repetitions. For instance, the base 10 representation of the repdigit 99999 is 9 10 5 1 10 1 display style 7times frac 105-110-1.
Brazilian numbers are a subset of repdigits that can be represented as a repdigit in various bases but exclude the repdigit 11 and mono values (or all the numbers will be Brazilian). For instance, the repdigit 33 in base 8 makes the number 27 a Brazilian number. In contrast, the repdigit 118, which is not permitted under the description of Brazilian numbers, makes the number 9 not a Brazilian number.
Creating Java programmes and learning about repdigit numbers are covered in this section.
To determine whether the provided value is a repdigit value or not. The repdigit number program is widely tested in academic settings and Java code interviews.
The Repdigit number
Repeated digits are abbreviated as repdigit. The sequence is A010785 from the OEIS. The repdigit is a numeric N that is equal and has the same number of digits inside its expression in base B.
Rep-units are the repdigits made up of repeated ones.
Example for the repdigits numbers
If the number 777 has a base of 10, then it is a repdigit number. In a similar way, 2000 will become a repdigit number if the specified base is 7 and the number in question. Because 2000 can be expressed as 5555 in base 7.
The initial repdigit numbers are as follows:
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 22, 33, 44, 55, 66, 77, 88, 99, 111, 222, 333, 444, 555, 666, 777, 888, 999, 1111, 2222, 3333, 4444,5555, 6666, 7777, 8888, 9999 etc. can be possibly considered as the repdigit numbers.
Finding a Repdigit Number: Steps
- Find each of the N digits in base B one by one.
- Every digit should be compared to its predecessor.
- Return false whether any value is not comparable to the previous digit.
- If not, return true.
Let's put the actions mentioned above into a Java program.
Java Program for Repdigit Number
We entered the number N into the software below to see whether it was a repdigit value is base (B) or not.
Let the file name of the program is RepdigitNumberDemo.java
import java.util.Scanner;
public class RepdigitNumberDemo
{
// function to determine whether or if a particular value is indeed a Repdigit number
static boolean theRepdigitNumber(int number, int s)
{
// given the starting value of -1 since it is the lowest possible digit, variable saves the preceding digit.
int previous = -1;
// from right to left, traverse the specified number in a loop.
while (number != 0)
{
// determines the given number's last digit
int lastdigit = number % s;
// divides by base the given number (s)
number = number/s;
// if previous is not -1, then execute
if (previous != -1 && lastdigit != previous)
return false;
previous = lastdigit;
}
return true;
}
//It is the driver code
public static void main(String args[])
{
int number, s;
Scanner sc=new Scanner(System.in);
System.out.print("Please Enter the number: ");
number = sc.nextInt();
System.out.print("Please Enter the base: ");
s = sc.nextInt();
if(theRepdigitNumber(number, s))
{
System.out.print("This number is a repdigit.");
}
else
{
System.out.print("It is not a number with repdigits.");
}
}
}
Output 1:
Please enter the number: 2000
Please Enter the base: 7
This number is a repdigit.
Output 2:
Please enter the number: 5555
Please Enter the base: 2
It is not a number with repdigits.
Output 3:
Please enter the number: 66666
Please Enter the base: 10
This number is a repdigit.