学无止境

Life-long learning
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

求质数

Posted on 2008-08-04 18:06  anytime8  阅读(150)  评论(0编辑  收藏  举报

using System;

class Factor
{
 public static void Main()
 {
  for(int i=1; i<=100; i++)
   if(IsPrime(i))
                Console.WriteLine(i);
 }

 public static bool IsPrime(int n)
 {
  for(int i=2; i<=Math.Sqrt(n); i++)
   if(n%i == 0)
    return false;

  return true;
 }
}