📜  最大素数 C# 代码示例

📅  最后修改于: 2022-03-11 14:48:44.560000             🧑  作者: Mango

代码示例1
static long LargestFactor(long n)
        {
            long lastFactor;
            if (n%2==0)
            {
                lastFactor = 2;
                n/=2;
                while (n % 2 == 0)
                    n/=2;
            }
            else
            {
                lastFactor = 1;
            }
            long factor = 3;
            long maxFactor = Convert.ToInt64(Math.Sqrt(n));
            while (n > 1 && factor <= maxFactor)
            {
                if(n % factor == 0)
                {
                    n/=factor;
                    lastFactor=factor;
                    while (n % factor == 0)
                        n/= factor;
                    maxFactor = Convert.ToInt64(Math.Sqrt(n));
                }
                factor += 2;
            }
            if (n == 1)
                return lastFactor;
            else
                return n;
        }