查找素数的程序

在csdn上看亡灵法师的帖子,突然找到一个好点的,如何找到素数。看了大家的讨论,参照registered等的算法,用.net写了一个。原帖连接在 这里
我运行后,效果如下:
1到200,000,000,一共找到了11078933个素数,花时间20秒(PIII 1G/512M)。代码如下,哪位有兴趣,可以对它进行优化。

None.gif using  System;
None.gif
using  System.Collections;
None.gif
None.gif
namespace  PrimerFind
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
class Class1
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{        
InBlock.gif        
private static bool[] storedprimes;
InBlock.gif        [STAThread]
InBlock.gif        
static void Main(string[] args)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            System.DateTime dt1 
= DateTime.Now;
InBlock.gif            Console.WriteLine(dt1.ToLongTimeString());
InBlock.gif
InBlock.gif            
int MAX = Convert.ToInt32(args[0]);
InBlock.gif            storedprimes 
= new bool[MAX];
InBlock.gif            storedprimes[
0= false;
InBlock.gif            storedprimes[
1= false;
InBlock.gif            storedprimes[
2= true;
InBlock.gif            
for(int i=3;i<MAX;i+=2)storedprimes[i] = true;
InBlock.gif            
int counter = MAX/2-2;
InBlock.gif
InBlock.gif            
int limit = (int)Math.Sqrt(MAX)+1;
InBlock.gif
InBlock.gif            
for(int prime = 3; prime < limit;) 
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
// Filter non prime number.
InBlock.gif
                int product = 0;
InBlock.gif                
for (int factor = prime; (product = factor * prime) < MAX; factor += 2
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
if (false != storedprimes[product]) 
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        storedprimes[product] 
= false;
InBlock.gif                        counter
--;
ExpandedSubBlockEnd.gif                    }

ExpandedSubBlockEnd.gif                }

InBlock.gif                
// Skip non prime number.
InBlock.gif
                do 
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    prime 
+= 2;
ExpandedSubBlockEnd.gif                }
 while (false == storedprimes[prime]);
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            
// TODO : 如果要输出,就把注释去掉
InBlock.gif
//            for(int i=0;i<MAX;i++)
InBlock.gif
//            {
InBlock.gif
//
InBlock.gif
//                //if(0 != storedprimes[i])Console.WriteLine(storedprimes[i]);
InBlock.gif
//            }
InBlock.gif

InBlock.gif            System.DateTime dt2 
= DateTime.Now;
InBlock.gif            Console.WriteLine(dt2.ToLongTimeString());
InBlock.gif            Console.WriteLine((dt2.Ticks
-dt1.Ticks)/10000);//毫秒
InBlock.gif
            Console.WriteLine(counter-2);
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值