Equidigital in Java
In this section, we will understand what is an equidigital number and how to write Java programs to locate them. It is commonly asked in academic settings and Java coding interviews.
Equidigital Number
A number n is referred to as being equidigital if somehow the number of points inside the factorization for n (including powers) equals the digit count in n.
For instance, the number 32 is very much an equidigital number because it has a prime factorization of 24 and a total of two digits in its prime factorization (2 and 4), which is equivalent to the number of digits in 32. Another illustration would be the number 64. Their prime factorization is 27 and it contains a total of 2 digits (2 and 6), whereas the number has 3 digits. There exist Equidigital primes for all numbers. To print every Equidigital number up to a given number, n, is the challenge. The term "equidigital number" in Java refers to a natural number that, including exponents, includes the same sequence of numbers as the digits included in its prime factorization.
Steps for Finding Equidigital Numbers
Step-1: First, choose a natural number. Let's assume that it is n.
Step-2: Calculate the number n's prime factorization in step two. The exponents of prime factorization and the digits that make up its prime components should be kept in an array.
Step-3: Determine the array's size.
Step-4: Determine how many digits there are for the number n.
Step-5: A total number is now an equidigital number whereas if entire number of digits it contains equals that size of the array discovered in step-3; otherwise, it is not.
Note: Remember that 1 cannot be an equidigital number. simply because 1 does not constitute a prime number and can only be divided by itself.
Program for Equidigital Numbers in Java
The procedures outlined above are used in the following software to check for equidigital numbers.
Let the name of the file is EquidigitalNumberDemo.java
// a demo program that looks for an equidigital number between 1 and 30
import java.util.*;
public class EquidigitalNumberDemo
{
// The procedure determines the exponents and numbers of the factors identified in // the prime factorization.
public static void addDigit(ArrList<Integer> sh, int num)
{
while(num != 0)
{
num = num / 10;
sh.add(1);
}
}
// a procedure for determining the all-prime factors of a given integer num, and for storing all
// those factors in the list sh
public static void findThePrimeFactors(int num, ArrList<Integer> sh)
{
// Remove all of the 2s from the number and add them to the list.
Boolean flag = false;
int count = 0;
while (num % 2 == 0)
{
flag = true;
num = num / 2;
count = count + 1;
}
if(flag)
{
al.add(2);
if(count > 1)
{
// manipulating the exponent digits
if(count > 9)
{
addDigit(sh, count);
}
else
{
sh.add(count);
}
}
}
// The number no must be odd after all of the 2s are subtracted from it. So, we can jump two spaces.
// Take note of the formula k = k + 2.
// Considering that 3 is odd and that 3 Plus 2 equals 5, which is also odd,
// It's also strange since 5 + 2 Equals 7, thus As a result,
// we can observe that adding 2 to that of an odd number always results in an odd number.
for (int k = 3; k <= Math.sqrt(num); k = k + 2)
{
count = 0;
flag = false;
// While k divides the number no, store j and continue dividing the number no
while (num % k == 0)
{
flag = true;
num = num / k;
count = count + 1;
}
if(flag)
{
// We must make sure that the primary elements
// are not taken into account.
// We must make sure that the components'
// entire amount of digits is taken into account.
if(k > 10)
{
addDigit(sh, k);
}
else
{
sh.add(k);
}
if(count > 1)
{
// handling exponent digits
if(count > 9)
{
addDigit(sh, count);
}
else
{
sh.add(count);
}
}
}
}
No should be added to the list when it's a
// prime number with a value larger than 2.
if(num > 2)
{
if(num > 10)
{
addDigit(sh, num);
}
else
{
sh.add(num);
}
}
}
// a technique to determine the total
// number of digits in the number num.
public static int findTheDigits(int num)
{
int digit = 0;
while(num != 0)
{
num = num / 10;
digit = digit + 1;
}
return digit;
}
// It is the main method
public static void main(String args[])
{
// List used to store all a number's prime factors
ArrList<Integer> sh = new ArrList<Integer>();
for(int s = 1; s <= 15; s++)
{
findThePrimeFactors(s, sh);
int len = sh.len();
int totalNoOfDigit = findDigits(s);
if(len == totalNoOfDigit)
{
System.out.println(i + " is a number with equidigits.");
}
else
{
System.out.println(i + " is not equidigital in nature.");
}
sh.clear();
}
}
}
Output:
1 is not equidigital in nature.
2 is a number with equidigital.
3 is a number with equidigital.
4 is not equidigital in nature.
5 is a number with equidigital.
6 is not equidigital in nature.
7 is a number with equidigital.
8 is not equidigital in nature.
9 is not equidigital in nature.
10 is a number with equidigital.
11 is a number with equidigital.
12 is not equidigital in nature.
13 is a number with equidigital.
14 is a number with equidigital.
15 is a number with equidigital.