判断一个数是不是素数,若不是,则找出其中一对因子

来源:互联网 发布:二次元pv制作软件 编辑:程序博客网 时间:2024/05/18 00:59

#include<iostream>
#include<cmath>
using namespace std;
int prime(int a)
{
int i;
if(a<=1){cout<<"error!"<<endl;return 1;}//若输入的数小于2 则报错
for(i=2;i<=int(sqrt(a));i++)// i的最大值为此数的根号2次方- -
{
if(a%i==0) //若有非1的因子,则跳出循环
break;
}
if(i>int(sqrt(a)))
cout<<"This number is a prime!"<<endl;
else
{
int j=a/i;
cout<<"This number is not a prime!"<<endl
<<"And two of its factors are "<<i<<" and "<<j<<endl;
}
return 0;
}
int main()
{
int a;
cout<<"Input a natural number,please! (n>1)"<<endl;
cin>>a;
prime(a);
return 0;
}
0 0