Skip to main content

Frugal Number

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.


function countDigits(n)
{
  var temp = n;
  var c = 0;
  while(temp != 0)
  {
    temp = parseInt(temp / 10);
    c++;
  }
  return c;
}

function frugal(n)
{
  var prime = seive(n);
  var r = [];
  for(var i = 2; i < n; i++)
  if(prime[i] === 1) r.push(i);
  var t = n;
  var s = 0;

  for(var i = 0; i < r.length; i++)
  {
    if (t % r[i] == 0)
    {
      var k = 0;
      while (t % r[i] === 0)
      {
        t = parseInt(t / r[i]);
        k++;
      }

      if (k === 1)
          s = s + countDigits(r[i]);
      else if (k != 1)
          s = s + countDigits(r[i]) + countDigits(k);
    }
  }

  return (countDigits(n) > s && s != 0);
}