×

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

  1. 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.
  2. 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

Economical number in Java

Related Topics

Hogben Numbers in Java

In this section, we will discover what the Hogben number is and develop Java programs that compute it. Java coding interviews and academic exams typically involve questions about the Hogben...

3 minutes read.

Maximizing Profit in Stock Buy Sell in Java

In this tutorial, we will deal with a popular problem, a favourite of interviewers. The problem is named as Maximising profit in stock Buy Sell. we will see certain approaches...

6 minutes read.

Comparator vs Comparable in Java

Comparator Vs Comparable in Java In Java, sorting of primitive data types can be done using different inbuilt functions that are available. But for sorting the collection of objects or types...

4 minutes read.

How to create an array in Java

An array is a linear data structure stores a collection of fixed number ofelements of similar type. The size of an array is specified when it is created. The array...

14 minutes read.

Java Swing Time Picker

Prerequisites In this tutorial, we will learn the time picker in java swing. Before learn time picker, we should learn about java swing. Java Swing Introduction The Java Foundation Classes include Java Swing....

5 minutes read.

Java program to print matrix in Z form

In this article, you will be acknowledged about what is a matrix along with an example. Also, most importantly, you will learn how to print matrix in Z form. What is...

5 minutes read.

Java Binary to Hexadecimal

Converting between types in programming is an important task. Moving from one kind to another kind conversion is occasionally necessary. We have discussed numerous conversion types in the section on...

3 minutes read.

How to find length of integer in Java

We can find the length of the integer in many ways. The length of an integer is defined as the count of the number of digits for the given integer. These...

5 minutes read.

Java Integer lowestOneBit()

The lowestOneBit () method of Java Integer class returns an int value with at most a single one-bit, in the position of the lowest-order one-bit in the specified int value.  Syntax public...

2 minutes read.

Brilliant Number in Java

It is a number N that is made up of two prime numbers that have the same number of digits and is called a brilliant number. Several/Some of the brilliant Numbers...

3 minutes read.

Java vs Golang

Java Java is both a programming language and a platform. Java is a high-level, dependable, object-oriented, and secure programming language. Java was developed in 1995 by Sun Microsystems, which is now an...

4 minutes read.

Reentrant Lock in Java

The Lock interface is implemented by the ReentrantLock class, which also provides methods for synchronising access to shared resources. The code that controls the resource has calls to the lock...

4 minutes read.

Difference Between BufferedReader and FileReader

BufferedReader: To read data from a specified character stream, two classes are used: buffered readers and file readers. Both of them have advantages and disadvantages. Although how they operate is the...

6 minutes read.

Java import packages

To know about the importing the packages of Java, we need to understand about how to packages work. Packages The package in Java is a collection of Classes and Interfaces. The packages...

3 minutes read.

Java Math exp() Method

The exp() method of Math class returns Euler’s number(e) raised to the power of a double value. Syntax: public static double exp(double a) Parameters: The parameter ‘a’ represents the exponent e. Return Value: The exp ()...

2 minutes read.

Java Integer valueOf() method

The valueOf() method of Java Integer class returns an Integer object holding the specified int value. The second method returns an Integer object holding the specified String value. The third syntax returns...

2 minutes read.

Types of Sockets in Java

The fundamental idea behind Java's networking capability is the socket. Early in the 1980s, the Berkeley UNIX 4.2BSD version included the socket paradigm. Berkeley socket is the term employed as...

9 minutes read.

What’s new in Java 12

On March 19th, 2019, the Java 12th edition was released. After releasing this edition, they have decided to release every new edition every six months. This version is the advanced...

5 minutes read.

Compile-time Error in Java

In java, the execution of a program is stopped due to the occurrence of some problem known as an error. Errors are illegal operations that are carried out by the...

4 minutes read.

What is the ambiguity problem in Java?

The ambiguity problem in Java occurs when a method or constructor is overloaded with two or more methods with the same name but different parameters. This can confuse when multiple...

6 minutes read.