求素数算法

class Program
    {
        static void Main(string[] args)
        {
            zhishuDemo(100);
        }

        /// <summary>
        /// 求质数(素数)
        /// </summary>
        /// <param name="n">范围</param>
        public static void zhishuDemo(int n)
        {
            List<int> list = new List<int>();
            list.Add(2);
            for (int i = 3; i < n; i++)
            {
                if (i % 2 == 0)
                {
                    continue;
                }
                int j = 3;
                while (j <= i / 2 && i % j != 0)
                {
                    j += 2;
                }
                if (j > i / 2)
                {
                    list.Add(i);
                }
            }
            list.ForEach(
                j =>
                {
                    Console.WriteLine(j);
                }
                );
            Console.ReadKey();
        }
    }
原文地址:https://www.cnblogs.com/shenyixin/p/2826251.html