Economical number in Java
Economical number is said to be economically efficient if the number of digits after prime factorization of the original number with their powers is less than the number of digits of the original number.
Examples of economical number
- 125 has a prime factorization of 5^3 and a total digit count of 2, including powers, which is fewer than the original digit count, making it an inexpensive number.
- 128 is not an economical number because there are 4 digits (2^2 * 3^3) in the prime factorization of the original number, which is more digits than the number of digits in the original number.
Steps to check whether the given number is economical or not
The steps to determine whether an amount is affordable are as follows:
- The primes up to 10^6 will be located in the first step.
- Calculate the original number's total digit count.
- Find the original number's prime factors, and for each prime factor, count the highest power of it that divides the original number. Then, calculate the total of the two powers.
- Return true if the sum of the digits in the prime factors is smaller than the sum of the digits in the initial number.
Program to check whether the given number is economical or not in Java
Economical.java
// importing the required packages
import java . util . * ;
import java . io . * ;
// A class is declared with name Ecomical
public class Economical
{
// declaring a static variable for storing maximum value
static int maxi = 10000 ;
// Using vector framework
static Vector < Integer > p = new Vector < Integer > ( ) ;
// Creating a static method with name PrimeValues to find the prime factors for the given //number
static void PrimeValues()
{
boolean [ ] mark = new boolean [ maxi / 2 + 1 ] ;
for ( int i = 1 ; i <= ( Math . sqrt ( maxi ) - 1) / 2 ; i++ ) {
for ( int j = ( i * ( i + 1 ) ) << 1 ; j <= maxi / 2 ; j = j + 2 * i + 1 )
{
mark [ j ] = true ;
}
}
// 2 should be added because it is a prime number
p . add ( 2 ) ;
// Add each additional prime that has the form 2*i + 1 and ensures that mark [ i ] is false.
for ( int i = 1 ; i <= maxi / 2 ; i++ )
{
if ( mark [ i ] == false)
{
p . add ( 2 * i + 1 ) ;
}
}
}
// static method with name checkEconomical is used to check whether the given number is economical or not
static boolean isEconomical ( int n )
{
//If the given number is 1, return false
if ( n == 1 )
return false ;
// Logic to find the number of digits in a given number
int realNumber = n ;
//initialize a variable with name count to store the number of digits of a number
int count = 0 ;
// while number greater than zero
while (realNumber > 0)
{
// incrementing the variable by 1
count ++ ;
// Removing the last digit in each iteration
realNumber = realNumber / 10 ;
}
// Declaring new integer variables to store prime count,primes
int primeFactDigits = 0 , New = 0 , primes = 0 ;
for (int i = 0 ; p . get(i) <= n / 2 ; i++) {
//
powers of prime in number count
while (n % p.get(i) == 0)
{
primes = p.get(i);
n = n / primes ;
// incrementing the variable new by 1
New++;
}
while (primes > 0)
{
// incrementing the variable primeFactDigits by 1
primeFactDigits++;
// removing the last digit from the number
primes = primes / 10;
}
while ( New > 1)
{
primeFactDigits++;
New = New / 10;
}
}
if (n != 1)
{
while (n > 0)
{
primeFactDigits++;
n = n / 10;
}
}
return (primeFactDigits < count) ;
}
// main method of the class where execution of the program starts
public static void main(String[] args)
{
int first, last;
// method to find all the prime values for the given number
PrimeValues();
// Creating Scanner class object to take the inputs from the user in a dynamic way during run time
Scanner sc= new Scanner(System.in);
// Asking the user to enter the starting range
System.out.print("Enter starting range: ");
// Storing the starting range value into integer variable first
first = sc.nextInt();
// Asking the user to enter the ending range
System.out.print("Enter ending range: ");
// Storing the ending range value into integer variable last
last = sc.nextInt();
for (int j = first; j < last ; j++)
{
// calling the function isEconomical to check whether the value is economical or not
if (isEconomical(j))
{
System.out.print(j + " is an economical number.\n");
}
}
}
}
Output
