首页 > 学院 > 开发设计 > 正文

求素数(质数)的算法

2019-11-18 16:49:49
字体:
来源:转载
供稿:网友
  class PRogram
    {
        static void Main(string[] args)
        {
            for (int i = 2; i <= 100; i++)
            {
                bool flag = true;
                int k =  Convert.ToInt32(Math.Sqrt(i));
                for (int j = 2; j <= k; j++)
                {
                    if (i % j == 0)
                    {
                        flag = false;                   
                        break;
                    }
                }
                if (flag == true)
                {
                    Console.WriteLine(i);
                }
            }
            Console.ReadLine();
        }
    }
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表