vonsy
传说中的水手
 
UID 281
精华
0
积分 42
帖子 42
阅读权限 1
注册 2005-8-8
状态 离线
|
Eratosthenes筛选素数_第二循环_数组元素值??false?
这段from <Programming in the key of c#> p161 Sieve of Eratosthenes(Eratosthenes筛选素数)
有一问题,请指教,谢谢.
见下面代码第二个for循环.
using System;
class SieveOfEratosthenes
{
static void Main()
{
Console.Write("输入想查找的素数范围: ");
int iMax = Int32.Parse(Console.ReadLine());
bool[] abIsPrime = new bool[iMax + 1];
for (int i = 0; i <= iMax; i++)
abIsPrime[i] = true;
for (int i = 2; i * i <= iMax; i++)
if (abIsPrime[i]) :书中说到:当外层循环把变量i设置为4时,因为
: abIsPrime[4]是false?? 上一个循环不是把
: abIsPrime元素都初始化成true了吗?
for (int j = 2; j <= iMax / i; j++)
abIsPrime[i * j] = false;
for (int i = 2; i <= iMax; i++)
if (abIsPrime[i])
Console.Write("{0} ", i);
Console.ReadLine();
}
} Eratosthenes(公元前276-公元前196)是埃及的一位天文学家,地理学家和诗人.
[ 本帖最后由 vonsy 于 2006-4-25 10:55 编辑 ]
|
|