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:

Output 2:
