算法 —— 判断是否为素数

判断:

if 素数 then 1

if 不是素数 then 最小因子

        public static int isPrime(long n)
        {
            bool isFind = false;
            for (int i = 2; i <= n - 1; i++)
            {
                if (n % i == 0)
                {
                    //不是素数
                    isFind = true;
                    break;
                }
            }
            if (!isFind)
            {
                return 1;
            }
            else
            {
                return Result.SmallestDivisor(n);
            }

        }

        public static int SmallestDivisor(long n, int d = 2)
        {
            if (n % d == 0)
                return d;
            return SmallestDivisor(n, ++d);
        }

  

原文地址:https://www.cnblogs.com/panpanwelcome/p/14999275.html