C# 如何使代码更优化、运行更快?

C# 如何使代码更优化、运行更快?,c#,C#,当变量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 &a

当变量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);
            }
        }
    }
公共静态int c=0;
公共静态无效TryAll(长x,长y)
{
对于(int i=2;i<10;i++)
{
双电源=数学功率(y,i);
如果(x%y==0&&powered==x&&x%10!=0)
{
C++;
Console.WriteLine(“----{0}”,c);
控制台。写入线(通电);
控制台写入线(y);
控制台写入线(i);
}
}
}
公共静态void Main(字符串[]args)
{
int baseNum=0;
对于(c=c;c<26;baseNum++)
{
如果(baseNum>9)
{
整数和=0;
int baseNumD=baseNum;
while(baseNumD!=0)
{
总和+=基数%10;
baseNumD/=10;
}
TryAll(baseNum,sum);
}
}
}

非常感谢您的帮助。

这将在大约70秒内找到所有小于或等于
int.MaxValue
的数字。调用
Math.Pow
可能会使您的性能下降,如果您尝试使用累加器和良好的旧乘法查找指数,则检查剩余的
%
是相当不必要的(实际上会使速度变慢):

static int SumOfDigits(int n)
{
    var current = n;
    var acc = 0;

    while (current > 0)
    {
        acc += current % 10;
        current /= 10;
    }

    return acc;
}

//Current implementation only works for positive n and b
//Easy to make it work with negative values, only need to
//perform some checks in absolute values.
static bool TryFindExponent(int n, int b, out int e)
{
    e = 0;

    if (b == 0 || n == 0)
        return false;

    //Check in abs if negative inputs are expected
    if (b == 1 &&
        n != 1)
        return false;

    var acc = 1L;

    do
    {
        e += 1;
        acc *= b;
    } while (acc < n) //check in abs if negative 
                      //numbers are expected;

    if (acc == n)
        return true;

    return false;
}

static void Main(string[] args)
{
    var sw = Stopwatch.StartNew();

    for (var i = 1; i < int.MaxValue; i++)
    {
        var b = SumOfDigits(i);

        if (TryFindExponent(i, b, out var e))
        {
            Console.WriteLine($"{i} = {b}^{e}");
        }
    }

    sw.Stop();
    Console.WriteLine($"Finished in {sw.ElapsedMilliseconds/1000.0} seconds.");
}

你能解释一下这个代码是做什么的吗?在阅读你所有的代码之前,试着理解它的实际功能,这会很有帮助。。。事实上,您可以使用
*=
代替
long-powered=y然后在循环中<代码>通电*=y这个问题可能更适合,但你需要解释更多关于代码的作用。我试图找到数字的数字总和的n次方等于数字本身的数字。如果这有意义的话。在for循环之前声明变量,然后在for循环中设置它们。这接近于我想要的,但不完全一样。我对我的代码进行了一点优化,它最多可以输出612220032=18^7。但我需要更进一步,所以我用long替换int变量,但正如您所想象的,这仍然需要几分钟的时间。如果81是x1,5832x5,614656x10等等,我需要一直到x25。
1 = 1^1
2 = 2^1
3 = 3^1
4 = 4^1
5 = 5^1
6 = 6^1
7 = 7^1
8 = 8^1
9 = 9^1
81 = 9^2
512 = 8^3
2401 = 7^4
4913 = 17^3
5832 = 18^3
17576 = 26^3
19683 = 27^3
234256 = 22^4
390625 = 25^4
614656 = 28^4
1679616 = 36^4
17210368 = 28^5
34012224 = 18^6
52521875 = 35^5
60466176 = 36^5
205962976 = 46^5
612220032 = 18^7
Finished in 73,075 seconds.