素数生成器的算法【C# EDITION】

 1public static ArrayList GeneratePrimes(int ubound)
 2        {
 3            BitArray flags = new BitArray(ubound + 1true);
 4            for(int i = 2; i <= (int)Math.Sqrt(ubound); i++)
 5            {
 6                if(flags[i])
 7                {
 8                    for(int j = i; j * i <= ubound; j++)
 9                    {
10                        flags[i * j] = false;
11                    }

12                }

13            }

14
15            ArrayList primes = new ArrayList();
16            for(int i = 2; i <= ubound; i++)
17            {
18                if(flags[i])
19                    primes.Add(i);
20            }

21            return primes;
22        }
posted @ 2005-05-08 10:39  三角猫  阅读(739)  评论(0编辑  收藏  举报