Frugal Number in Java
In this section, we will learn what is a frugal number and also create Java programs to check if the given number is a frugal number or not. The frugal number program is frequently asked in Java coding interviews and academics.
A frugal number is a natural number n with base-b whose number of digits is greater than the number of digits in its prime factorization (including exponents greater than 1). It is an OEIS sequence A046759.
Points to remember
We can say that frugal number is the same as the economical number. Hence, we can represent frugal numbers as write n as a product of primes raised to powers.
d(n): Represents the number of digits in the product.
l(n): Represents the number of digits in n.
Therefore, sequence gives n such that d(n)<l(n).
Let's understand it through the examples.
Frugal Number Example
125 = 53, l(n) = 3 and d(n) = 2. Here, d(n)<l(n), so 125 is a frugal number.
128 = 27, l(n) = 3 and d(n) = 2. Here, d(n)<l(n), so 128 is a frugal number.
243 = 35, l(n) = 3 and d(n) = 2. Here, d(n)<l(n), so 243 is a frugal number.
256 = 28, l(n) = 3 and d(n) = 2. Here, d(n)<l(n), so 256 is a frugal number.
2048 = 211, l(n) = 4 and d(n) = 3. Here, d(n)<l(n), so 2048 is a frugal number.
We observe that the number written with k digits, while its factorization of the form pq uses digits greater than k.
Similarly, we can check other numbers also.
Some other frugal numbers are in base 10 are 343, 512, 625, 729, 1024, 1029, 1215, 1250, 1280, 1331, 1369, 1458, 1536, 1681, 1701, 1715, 1792, 1849, 1875, 2048, 2187, 2197, 2209, 2401, 2560, 2809, 3125, 3481, 3584, 3645, 3721, 4096, 4374, 4375, 4489, 4802, 4913.
The first frugal number that is not a prime power is 1029 = 3 × 7³.
Frugal Number Java Program
Enter the number to check: 125 It is a frugal number.
Enter the number to check: 2084 It is not a frugal number.