So I'm asked to write a program that outputs the first N number of frugal numbers.
A frugal number is a number that has more digits than the digits in its prime factorization
(including the number of digits in the powers)
Ex: 1024 = 210
1024 is frugal since it has 4 digits, whereas 210 has 3 digits (2digits in the power).
However in the example above, 225 has 4 digits in its prime factorization and therefore is not frugal.

For example the frugal numbers up to 2000 are:
125, 128, 243, 256, 343, 512, 625, 729, 1024, 1029, 1215, 1250, 1280, 1331, 1369, 1458, 1536, 1681, 1701, 1715, 1792, 1849, 1875

So here is the problem, I cant figure out what have I done wrong, I'd appreciate any help given.

#include <iostream>
#include <string>

using namespace std;
bool IsFrugal(int c)
{int icount = 0, digitCount = 0;

for (int i=2; i <= c; i++)
{
 while(c % i == 0)
 {
   c /= i;
   icount++;
 }
}
{while ( c > 0 )
  {         
    c = c / 10;
    digitCount++;
  }
}
if (digitCount>icount)
	return true;
else
	return false;
}
void main()
{
	int num;
	cout << "Enter a number: ";
	cin >> num;
	for(int c=1; c<=num; c++)
	{
		if(IsFrugal(c))
			cout<<c;
	}
	cout<<endl;
}

Edited 5 Years Ago by Ezzaral: Added code tags. Please use them to format any code that you post.

So here is the problem, I cant figure out what have I done wrong, I'd appreciate any help given.

Please use CODE tags and indent your code; this will make it easier for us to read.

Also, it helps if you tell us what "wrong" means--what are you expecting to happen, and how is what's actually happening different?

At a first glance, this code looks suspicious:

for(int i = 2; i <= c; i++)
{
    while(c % i == 0)
    {
        c /= i;
        icount++;
    }
}

It looks like you're counting the number of prime factors, not the total number of digits across all prime factors, &c.

This article has been dead for over six months. Start a new discussion instead.