您的位置:首页 > 编程语言 > C#

用C#语言求300以内的素数

2015-10-04 23:09 471 查看
程序思想是用这个数去除2到sqrt(这个数),如果能被整除,就不是素数。斜体加粗部分就是定义一个IsSuShu(int x)函数来判断是否整除。

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespace _90

{

class Program

{

***static private Boolean IsSuShu(int x)

{

Boolean Yes = true;

for (int i = 2; i < Math.Sqrt(x); i++)

{

if ((x % i) == 0)

{

Yes = false;

}

}
return Yes;
}***

static void Main(string[] args)
{
for(int i=2;i<300;i++)
{
if(IsSuShu(i)==true)
{
Console.Write(i.ToString());
Console.Write(" ");
}
}
Console.ReadKey();
}
}


}

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: