Eratosthenes筛选素数_第二循环_数组元素值??false?
这段from <Programming in the key of c#> p161 Sieve of Eratosthenes(Eratosthenes筛选素数)
有一问题,请指教,谢谢.
见下面代码第二个for循环.
[code]
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();
}
}
[/code]
Eratosthenes(公元前276-公元前196)是埃及的一位天文学家,地理学家和诗人.
[[i] 本帖最后由 vonsy 于 2006-4-25 10:55 编辑 [/i]] |