c#:筛法求素数(bool数组)

  1. using System;

  2. public class PrimeFilter{

  3.     public static void Main( string [] args ){

  4.         int N = 100;

  5.         bool [] a = new bool[N+1];

  6.         forint i=2; i<=N; i++ ) a[i]=true;

  7.   

  8.         forint i=2; i<N; i++ )

  9.         {

  10.             if(a[i]) forint j=i*2; j<=N; j+=i )

  11.                 a[j]=false;

  12.         }

  13.   

  14.         forint i=2; i<=N; i++) 

  15.             if( a[i] ) Console.Write( i + " " );

  16.     }

  17. }

posted @ 2019-07-25 16:57  MichaelCecil  阅读(993)  评论(0编辑  收藏  举报