求一个判断素数的算法

来源:百度知道 编辑:UC知道 时间:2024/06/17 14:40:15
问题是这样的:
用拉普拉斯算法写一个判断素数的算法
如果是,输出YES,不是就输出NO。
也就是计算机算法设计第三版的第七章算法实现题7-2
知道的高手棒棒忙谢谢
把源代码附上,要能通过编译的,拜托了,急用!!

如果不会拉普拉斯用别的方法可以做的也告诉我看看!

#include<iostream>
using namespace std;
void main()
{
int n,i=0;
cin>>n;
for(int j=1;j<=n/2;j++)
{
if(n%j==0)
{
i++;
}
}
if(i==0)
cout<<"YES"<<endl;
else
cout<<"NO"<<endl;
}

#include <stdio.h>

int function (int a)
{
int j;
for (j=2;j<a;j++)
if (a%j==0)
return 0;
return 1;
}

void main ()
{
int i,k;
printf("enter a number:\n");
scanf("%d",&i);
if(function(i))
printf("是素数");
else printf("不是素数");
getchar();
}