×

Frugal number in Java

A frugal number is a positive integer with base b that has more digits than the number of prime factors it can be factored into (including exponents greater than 1). It has OEIS sequence number A046759.

Remember:

  • Frugal and equidigital numbers combined produce expensive results.
  •  A sum of two frugal integers can represent any number bigger than 1923. The economical figures are so-called by some academics.

A number with more characters than the number of characters inside its prime numbers is said to be frugal. The number of digits inside the prime numbers is not affected if the exponents for just a particular prime involved inside the prime numbers equals 1.

Examples of frugal numbers:

1) 125 = 53, where the integer has 3 bits (1, 2, and 5), which is more than just the number of decimal places required to calculate its prime numbers, which is two (5 and 3).

2) 512 = 29, where the number has three digits, five, one, and two, which is more than the two - digits required for its prime numbers (2 and 9).

3) The integer 1029 has four digits (1, 0, 2, and 9), which is longer than the 3 digits required for its factoring of large numbers (3 73). (3, 7, and 3).

Following are the beginning few frugal numbers:

Examples:125, 128, 243, 256, and 343.

Because the amount of characters in prime numbers of a positive integer equals the sequence of numbers inside the prime number, it should be emphasized that primes are not thrifty numbers (since exponents of value 1 are not considered).

Example 19 = 191; however, the exponent's 1 does not count toward the number of digits needed to divide the number into its prime factors. Consequently, the number has two digits (1 and 9), matching the number of digits in its prime factorization (1 and 9).

Procedure for finding the frugal number

Simple procedures are used to determine if the number 'n' is economical. The prime factorization of n is first determined by finding all prime figures up to 'n'. Finally, we determine if n has more digits than the number of numbers in its factorization.

Java program for a frugal number

Ex: FrugalExample.java

// This program is for Frugal number
import java.io.*;  
import java.util.*;  
public class FrugalExample 
{  
//method for checking whether the number is a frugal number or not  
static boolean isFrugal(long n)  
{  
ArrayList<Long> arrays = primes(n);  
long t = n;  
//digits which are there in the prime factorization of a number 
long s = 0;  
for (int i = 0; i < arrays.size(); i++)  
{  
if (t % arrays.get(i) == 0)  
{  
//find the exponent for the number 
long k = 0;  
//iterating the loop till it divides the prime factor  
//it will determine the exponent  
while (t % arrays.get(i) == 0)  
{  
t = t / arrays.get(i);  
k++;  
}  
//finding the number of digits in the exponent  
if (k == 1)  
s = s + countDigits(arrays.get(i));  
else if (k != 1)  
s = s + countDigits(arrays.get(i)) + countDigits(k);         
}  
}  
// if the condition becomes true, then it will return 
return (countDigits(n) > s && s != 0);  
}      
// this function finds the prime numbers upto the numbers  
static ArrayList<Long> primes(long n)  
{  
//a boolean array will be created for the prime numbers      
boolean []prime = new boolean[(int)n + 1];  
for(int i = 0; i < n + 1; i++)  
prime[i] = true;  
//the prime number is determined by the method  
for (int i = 2; i * i <= n; i++)  
{  
//if the condition becomes true, then it returns a prime number  
if (prime[i] == true)  
{  
//it will search the prime numbers 
for (int j = i * 2; j <= n; j += i)  
prime[j] = false;  
}  
}  
//An array consisting of a list of prime numbers  
ArrayList<Long> array = new ArrayList<Long>();  
for (int i = 2; i < n; i++)  
//if the condition becomes true return prime
if (prime[i])  
//the elements are pushed to the array 
array.add((long)i);  
//returns an array of prime numbers  
return array;  
}  
//function for counting the number of digits 
static int countDigits(long n)  
{ 
long temp = n;  
int count = 0;  
while (temp != 0)  
{  
// the given number is checked by diving 10   
temp = temp / 10;  
//the value of count is increased by 1 
count++;  
}  
//returning the count of the digits
return count;  
}  
// main section of the program
public static void main(String args[]) throws IOException 
{  
Scanner sc = new Scanner(System.in);  
System.out.print("Please enter the number to check: ");  
long num = sc.nextLong();  
// the isFrugal method is called 
if (isFrugal(num))  
System.out.println("The entered number "+num+" is a frugal number");  
else  
System.out.println("The entered number "+num+" is not a frugal number");  
}  
}  

Output 1:

Frugal number in Java

Output 2:

Frugal number in Java

Related Topics

Constructor Chaining and Constructor Overloading in Java

Constructor Chaining Constructor chaining and constructor overloading are two confusing terms. Let's first understand constructor chaining.Constructor chaining is the process of calling one constructor from another constructor using the same object....

2 minutes read.

Callable Statement in Java

The Callable statement in Java is used to call the functions and Stored procedures. Example: If we want to know about the age of a person based on their date of birth,...

3 minutes read.

Hamming Code in Java

In a computer network, hamming code is a unique set of error-correction codes. It is mostly utilised in computer graphics for mistake detection and correction during data transmission from sender...

8 minutes read.

Palindrome number program in Java

Write java program to check if a number is palindrome in Java? A number that does not change on reversing is called a palindrome number. In other words, when reversing the...

3 minutes read.

How to Update Java

As we all know that java can be installed in all operating systems like windows, Linux, macOS. We are available with the java 17 and java 18 versions in the...

3 minutes read.

How to compare three dates in Java?

While using the date and the time in Java, we occasionally have to compare the dates. Java does not compare dates the same way it compares the two numbers. Therefore,...

6 minutes read.

Java Integer toString() method

The toString() method of Java Integer class returns a String object which represents this Integer’s value. The second syntax returns a String object which represents the specified integer. The third syntax returns...

2 minutes read.

Java Read File

Java provides its users with many ways of reading a file. To read a text file, you can use a FileReader, BufferedReader, or Scanner. Each utility offers something special for...

6 minutes read.

Monsoon Umbrella Problem in Java

The Monsoon Umbrella problem is a classic Java programming problem used to test the skills of a programmer. The problem involves writing a program to determine the number of umbrellas...

3 minutes read.

Java program to find frequency of characters in a string

In this article, you will understand the how to find the frequency of characters in strings by using Java programming language. Along with this, you will understand the hashing concept...

3 minutes read.

Synchronized Keyword in Java

Synchronization is the process of limiting access to a shared resource or data to a single thread at a given moment in time. This aids in shielding the data from...

6 minutes read.

Nested Enum in Java

A class that can be defined within another class is called a nested class in Java. You can logically group classes that are used onlyin one place. This makes encapsulation...

3 minutes read.

Differences between Lock and Monitor in Java Concurrency

In this tutorial, we will discuss the overview of Lock and Monitor and the differences between them. Introduction Java Concurrency is the ability to perform specific tasks at a time parallelly. The...

4 minutes read.

Centered Square Numbers in Java

In this tutorial, we will understand how to check the number is a centered square number. It is one of the prevalent interview questions of IT companies. Firstly, we will...

3 minutes read.

Heap Sort in Java

Heap Sort in JavaHeap sort in Java uses the data structure binary heap, min-heap, or max heap to do the sorting of elements. Since min-heap always gives the minimum element first,...

8 minutes read.

How to Convert Decimal to Binary in Java

How to Convert Decimal to Binary in Java There are two methods to convert Decimal to Binary. Using toBinaryString() method Using user-defined logic Using Integer.toBinaryString() The toBinaryString() is a static method of Integer...

2 minutes read.

Example of Static import in Java

Static keyword Java's static keyword is mainly used to control memory. Variables, methods, blocks, and nested classes are compatible with the static keyword. Instead of an instance, the static keyword is...

3 minutes read.

Print Pencil Shape Pattern in Java

Another pattern made from asterisk symbols that use loops and other logical concepts is the pencil pattern. It is usually requested to draw a pattern using a program. To write the...

6 minutes read.

Fibonacci Series Program in Java

Fibonacci Series Program in Java using Recursion Fibonacci series is a series whose every term is comprised of adding its previous two terms, barring the first two terms 0 and 1....

3 minutes read.

Interleaving string in Java

If the string Str3 contains all of the characters from Str1 and Str2, it is considered interleaving Str1 and Str2. Keep in mind that the order of all characters in...

5 minutes read.