我编写此代码是为了找到给定数字的除数。我尝试实现的方法找到所有素数因子(有效)并取相似素数的数量加一(给出除数的数量)。
例如 28 = 2*2 * 7 --> (2+1)*(1+1) = 6
这是我的尝试:
int num = 20;
int next = 0;
int exponent = 0;
int numberOfDivisors = 1;
start:
for (int i = 2; i <= num; i++)
{
next = i;
if (num%i == 0)
{
if (i == next)
{
exponent++;
}
else
{
numberOfDivisors *= (exponent+1);
exponent = 0;
}
if (num != i)
{
num /= i;
goto start;
}
}
}
std::cout << numberOfDivisors << std::endl;
我只是无法弄清楚我错过了什么。