×

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

Java Package Keyword

A collection of classes, interfaces, and subpackages in a Java program are referred to as a package. Here, we'll learn in-depth how to make and use user-defined packages. package is a...

6 minutes read.

Buffer reader to read string in Java

The Buffered Reader class of Java is used to read the stream of characters from the input stream. Program to read string using Buffer reader import java.io.*; class  Demo {   public static void main(String...

3 minutes read.

Star Pattern Programs in Java

Star Pattern Programs in Java The star pattern programs in Java is the part of pattern programs in Java, which we discussed earlier. Right Triangle Star Pattern Filename: StarPatternExample.java public class StarPatternExample {              public static void...

4 minutes read.

Java float vs double

Java : Java is a pure object oriented language. It was introduced by James Gosling in the year 1995. The first public implementation of java was done by sun micro systems...

7 minutes read.

Getting the Day from the Date in Java?

To extract the day's name from the date, we will write Java application. When dealing the date and time in Java, the following classes are used. Class for Calendars: This class is...

6 minutes read.

Java Identifiers

In Java, the symbolic notations used for identification are called identifiers. Identifiers can be the name for the class, variable name declaration, name of the package, constant name and many...

3 minutes read.

Facts about null in Java

Nearly all programming languages have a relationship with null. Hardly any programmers are unconcerned by null. The null has a java.lang.NullPointerException association in Java. Given that it is a class...

4 minutes read.

Dangling Else problem in Java

A language interpretation uncertainty is the hanging other issue. The following two types of condition executed code are both possible in programming: 1. if-then-else form 2. if-then form When dealing with the nested...

3 minutes read.

Java 8 Features

Java 8 Features: After the great success of Java 7, many developers were waiting for the next version of one of the best languages in the tech world. Moreover, on...

6 minutes read.

Normal and Trace of a Matrix in Java

Normal of a matrix The square root of the total squares of each element in a matrix is known as the matrix's normal. Think of the following matrix as an example. Example: [[1,2,3]  ...

3 minutes read.

Java Extends keyword

Extends Extends is a keyword which is completely depended on the concept of the inheritance of the java programming language.To understand about of the keyword, we need to learn the concept...

3 minutes read.

Web Service Response Time Calculation in Java

Administration response time or reaction time is the typical measure of time it takes for a solicitation to be handled by a PC framework, like your organization switch. In the...

4 minutes read.

How to Split String by Comma in Java

strsplit() technique permits you to break a string given the explicit Java string delimiter. The Java string split property is frequently a space or a comma(,) that you want to...

7 minutes read.

Functional Interface in Java 8

A brief introduction to Interface in Java: Interfaces in Java are basically the blue print of classes. Before the appearance of Java 8, it was only possible to declare one or...

11 minutes read.

Java Program to Sort an Array of 0's, 1's, and 2’s | Dutch National Flag Problem in Java

The famed Dutch computer programmer Edsger Dijkstra's Dutch Nation Flag (DNF) challenge ranks among the most well-known programming challenges. The Dutch tricolor flag, comprising red, white, and blue, is the...

3 minutes read.

How to open the Java control panel

The many methods for launching the Java control panel will be covered in this section. We will also go through how the Java Control Panel can be used. Java Control Panel The...

2 minutes read.

Non-primitive data types in Java

The kind of data stored in the variable is determined by its type. The type describes the data category (different sizes and values). These are not already built into the devices....

4 minutes read.

Difference Between Access Specifiers and Modifiers in Java

Java employs access modifiers to restrict a class's data members, member functions, and constructor. Access modifiers are essential when creating Java program and applications. Access modifiers in Java include: defaultpublicprotectedprivate Default Access Modifiers Without...

4 minutes read.

Ramanujan Number or Taxicab Number in Java

In this section, we will discuss what a Ramanujan number (also known as a Hardy-Ramanujan number) is and how to use a Java programme to determine if a given integer...

3 minutes read.

How Many Ways to Create Objects in Java

Introduction Java comes under the category of object-oriented programming languages. Since it is object-oriented, everything in Java is considered an object. Java is a diverse programming language that is designed to...

6 minutes read.