基本数学算法

1)判断素数
算法:
从p:2~sqrt(n),依次判断n/p的余数
实现:
#include <cmath>
using namespace std;

bool IsPrime(int n)
{
    
int i;
    
for(i = 2; i <= sqrt(n); i++)
    {
         
if(n % i == 0)
             
return false;
    }
    
    
return true;
}
posted @ 2007-07-20 19:10  中土  阅读(483)  评论(0编辑  收藏  举报
©2005-2008 Suprasoft Inc., All right reserved.