当变量c达到15左右时,下面的代码运行得非常慢 . 我没有看到过去16,我需要它一直到25 .
public static int c = 0;
public static void TryAll(long x, long y)
{
for (int i = 2; i < 10; i++)
{
double powered = Math.Pow(y, i);
if (x % y == 0 && powered == x && x % 10 != 0)
{
c++;
Console.WriteLine("----- {0}", c);
Console.WriteLine(powered);
Console.WriteLine(y);
Console.WriteLine(i);
}
}
}
public static void Main(string[] args)
{
int baseNum = 0;
for (c = c; c < 26; baseNum++)
{
if (baseNum > 9)
{
int sum = 0;
int baseNumD = baseNum;
while (baseNumD != 0)
{
sum += baseNumD % 10;
baseNumD /= 10;
}
TryAll(baseNum, sum);
}
}
}
任何帮助表示赞赏 .
1 回答
这将在大约70秒内找到最多
int.MaxValue
的所有数字 . 调用Math.Pow
可能会让你的性能下降并检查余数%
是不必要的(它实际上使它变慢)如果你试图找到带有累加器和良好的旧乘法的指数:这将在我的机器中输出: