×

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

Traverse through HashMap in Java

In this tutorial, we will discuss traversing through HashMap in Java Introduction: HashMap<Key, Value> is a part of the java collection implemented from the java 1.2 version. HashMap is written as HashMap<K,...

4 minutes read.

Java Program to remove duplicate characters in a string

In strings, we can find many characters present one or more times in a string; accessing a particular character is difficult due to repetition. Duplicate characters will present in the...

5 minutes read.

Producer Consumer Problem in Java Using Synchronized Block

The producer-consumer dilemma is a well-known instance of a multi-process synchronization issue in computing. Two processes—the producer and the consumer—are described in the issue, and they share a single, fixed-size...

4 minutes read.

Magic Square in Java

A square with numbers is referred to as a magic square. The numbers in a magic square of order n are arranged to ensure that the sum of all of...

4 minutes read.

Java Map Generic

Java arrays maintain an ordered collection of things, and the index can be used to access the data (an integer). Unlike HashMap, which stores data as a Key/Value pair. We...

3 minutes read.

List of Constants in Java

In this tutorial, we are going to deal with constants available in Java.Every programming language has its constants. Similarly, Javaalso has got constants of its own. In this tutorial, we will...

6 minutes read.

Java Clone Array

We frequently need to copy an array to back up its original components. We have a few unique numbers and strings, including Armstrong numbers, palindrome numbers, and palindrome strings. To...

4 minutes read.

Self-Descriptive Numbers in Java

A number n is given. Identifying the self-descriptive numbers that exist between 1 and n is our task. Self-Descriptive Numbers The definition of a self-descriptive number, m, is a number with b...

6 minutes read.

Java Code Optimization

We encounter the idea of optimization while working on any Java application. It is essential that the code we write is not only clear and error-free but also optimized, meaning...

9 minutes read.

Java Primitive Data Types

Primitive data types are the simplest data types in a programming language. They’re predefined in the language. The names of the primitive types are quite descriptive of the values that...

2 minutes read.

Difference Between Java and PHP

The two most used programming languages are PHP and Java. Both of them have a lot of similarities and distinctions. Let's first grasp each of them individually before examining their...

3 minutes read.

Sorting Algorithms in Java

Sorting Algorithms in Java Sorting is the technique that puts the elements of an array or list either in descending or ascending order. For example, take an array A, whose elements...

3 minutes read.

How to add 6 Months to the Current Date in Java?

In this tutorial, we will learn how to add 6 months to the local or current date in Java language. We will begin our topic with basic concepts and would...

3 minutes read.

Java Map Interface

A map is a collection that maps keys to values, with no duplicate keys allowed. The elements in a map are key/value pairs. HashMap: HashMap stores the keys in a...

3 minutes read.

Java RMI

What is RMI in Java? A framework for developing distributed Java applications is provided by the RMI (Remote Method Invocation) API. An object can call methods on an object running in...

4 minutes read.

Round Robin Scheduling Program in Java

A CPU scheduling technique is known as Round Robin (RR). Additionally, network schedulers employ it. It was created specifically for a time-sharing system. The temporal slicing scheduling algorithm is another...

4 minutes read.

Deadlock Prevention and avoidance in Java

This tutorial will discuss deadlock prevention and Avoidance in Java programming language. Introduction Multithreading in Java includes deadlock. We can multitask by running several threads concurrently in the multithreading environment. Deadlock occurs...

4 minutes read.

Java Integer numberOfTrailingZeros() method

The numberOfTrailingZeros()  method of Java Integer class returns the total number of zero bits following the lowest-order one-bit in the 2’s complement binary representation of the specified int value. Syntax public static...

1 minute read.

How to Convert Timestamp to Date in Java

How to Convert Timestamp to Date in Java You can convert Timestamp to Date by using the constructor of Date class. It returns the long millisecond from Epoch (1st January 1970)...

2 minutes read.

Find next greater number with same set of digits in Java

It contains a number (num). Finding the smallest number that has the same number of elements as num that is also larger than num is the task at hand. If...

8 minutes read.