求素数(质数)的算法
2006-12-08 17:17:44 来源:WEB开发网核心提示: 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 (i
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();
}
}
{
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();
}
}
- ››算法大全(3) 二叉树
- ››算法
- ››算法从哪学起
更多精彩
赞助商链接