A frugal number is a number whose number of digits is strictly greater than the number of digits in its prime factorization (including exponents). If the exponent is 1 for a certain prime, involved in the prime factorization, then that exponent does not contribute to the number of digits in the prime factorization.
Some examples of frugal numbers are:
1) 125 = , here the number of digits in the number is three (1, 2 and 5) which is strictly greater than the number of digits in its prime factorization which is two (5 and 3).
2) 512 = , here the number of digits in the number is three (5, 1 and 2) which is strictly greater than the number of digits in its prime factorization which is two (2 and 9).
3) 1029 = 3 × , here the number of digits in the number is four (1, 0, 2 and 9) which is strictly greater than the number of digits its prime factorization which is three (3, 7 and 3).
The first few frugal numbers are : 125, 128, 243, 256, 343, 512, 625, 729, ….
It may be noted here that prime numbers are not frugal numbers, since the number of digits in the prime factorization of a prime number is equal to the number of digits in the prime number (since exponents of value 1 are not considered).
Example 19 = , but the 1 in the exponent does not contribute to the number of digits in the prime factorization of the number. Hence the number of digits in the number is two (1 and 9), which is equal to the number of digits in its prime factorization (1 and 9).
A program to find whether a number, ‘n’ is frugal or not involves simple steps. First, we find all prime numbers upto ‘n’ and then find the prime factorization of n. Finally, we check whether the number of digits in n, is greater than the number of digits in the prime factorization of n.
# Program to check for Frugal number
# Finding primes upto entered number
# Finding primes by Seive
# of Eratosthenes method
prime = [True] * (n + 1);
i = 2;
while (i * i <= n): # If prime[i] is not changed, # then it is prime if (prime[i] == True): # Update all multiples of p j = i * 2; while (j <= n): prime[j] = False; j += i; i += 1; # Forming array of the prime # numbers found arr = ; for i in range(2, n): if (prime[i]): arr.append(i); return arr; # Returns number of digits in n def countDigits(n): temp = n; c = 0; while (temp != 0): temp = int(temp / 10); c += 1; return c; # Checking whether a number is # Frugal or not def frugal(n): r = primes(n); t = n; # Finding number of digits # in prime factorization # of the number s = 0; for i in range(len(r)): if (t % r[i] == 0): # Exponent for current factor k = 0; # Counting number of times # this prime factor divides # (Finding exponent) while (t % r[i] == 0): t = int(t / r[i]); k += 1; # Finding number of digits # in the exponent Avoiding # exponents of value 1 if (k == 1): s = s + countDigits(r[i]); elif (k != 1): s = (s + countDigits(r[i]) + countDigits(k)); # Checking condition # for frugal number return (countDigits(n) > s and s != 0);
# Driver Code
n = 343;
print(“A Frugal number”);
print(“Not a frugal number”);
# This code is contributed by
A Frugal number
The above code can be optimized using the approach discussed in Print all prime factors and their powers
- Count number of trailing zeros in Binary representation of a number using Bitset
- Count number of triplets with product equal to given number with duplicates allowed
- Find minimum number to be divided to make a number a perfect square
- Given number of matches played, find number of teams in tournament
- Number of possible permutations when absolute difference between number of elements to the right and left are given
- Number which has the maximum number of distinct prime factors in the range M to N
- Number of times the largest perfect square number can be subtracted from N
- Check if a number is divisible by all prime divisors of another number
- Count the number of operations required to reduce the given number
- Super Ugly Number (Number whose prime factors are in given set)
- Find the number of integers x in range (1,N) for which x and x+1 have same number of divisors
- Program to Convert Octal Number to Binary Number
- Check whether all the rotations of a given number is greater than or equal to the given number or not
- Count Number of animals in a zoo from given number of head and legs
- Find count of digits in a number that divide the number
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.