输出N以内素数的方法

最近有个朋友问我"输出N以内素数的方法", 我想网上一定有很多方法, 而且代码有可能很雷同. 不过还是自己写了一段.

贪心法明显要快很多. 

ExpandedBlockStart.gif 代码
using  System;
using  System.Collections.Generic;
using  System.Linq;
using  System.Text;

namespace  ConsoleApplication2
{
    
    
class  Program
    {
        
// 普通方法
         public   static  List < int >  GetPrimeList( int  maxNum)
        {
            List
< int >  primeList  =   new  List < int > ();
            primeList.Add(
2 );
            
for  ( int  onCheck  =   3 ; onCheck  <=  maxNum;  ++ onCheck)
            {
                
bool  isPrime  =   true ;
                
for  ( int  checker  =   2 ; checker  <=  Math.Sqrt(onCheck);  ++ checker)
                {
                    
if  ( 0   ==  onCheck  %  checker)
                    {
                        isPrime 
=   false ;
                        
break ;
                    }
                }
                
if  ( true   ==  isPrime)
                {
                    primeList.Add(onCheck);
                }
            }

            
return  primeList;
        }

        
// 贪心法
         public   static  List < int >  GetPrimeList( int  maxNum,  bool  useGreedy)
        {
            List
< int >  primeList  =   new  List < int > ();
            
if  ( true   ==  useGreedy)
            {
                
bool [] map  =   new   bool [maxNum  +   1 ];
                
for  ( int  i  =   2 ; i  <=  maxNum;  ++ i)
                {
                    map[i] 
=   true ;
                }
                
for  ( int  i  =   2 ; i  <=  Math.Sqrt(maxNum);  ++ i)
                {
                    
if  ( true   ==  map[i])
                    {
                        
for  ( int  j  =  i * i; j  <=  maxNum; j  +=  i)
                        {
                            map[j] 
=   false ;
                        }
                    }
                }
                
for  ( int  i  =   2 ; i  <=  maxNum;  ++ i)
                {
                    
if  ( true   ==  map[i])
                    {
                        primeList.Add(i);
                    }                    
                }
            }
            
else
            {
                primeList 
=  GetPrimeList(maxNum);
            }

            
return  primeList;
        }
        
static   void  Main( string [] args)
        {
            
int  timeStart  =  DateTime.UtcNow.Minute  *   60   +  DateTime.UtcNow.Second;
            List
< int >  primes  =  GetPrimeList( 100000000 , true );
            
int  timeEnd  =  DateTime.UtcNow.Minute  *   60   +  DateTime.UtcNow.Second;
            
// foreach (int item in primes)
            
// {
            
//     Console.Write("{0},", item);
            
// }
            
// Console.Write("\n");
            Console.WriteLine( " primes cont: {0}, used {1} second " ,primes.Count, timeEnd - timeStart);
            Console.Read();
        }
    }
}

 

 

转载于:https://www.cnblogs.com/yakashop/archive/2010/04/20/1716106.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值