素數的實現,質數,判斷一個數是不是素數

/******************************************************************
@author:xuquan
data:20150721
function:判斷一非負整數是不是素數
*******************************************************************/


#include<stdio.h>
#include<math.h>
#include<stdlib.h>


int main(void)
{
long long int m,i;
// scanf("%d",&m); //VS2013已下版本或者ubuntu
puts("請輸入一個非負數進行判斷,該數是不是素數:");
scanf_s("%lld",&m);  //VS2013以上版本
long long int count = (long long)sqrt(m*1.0);   //縮小循環次數,簡單優化判斷素數的條件
if (m == 0 || m == 1)
{
printf("%lld 不是一個素數!\n", m);
system("pause");
exit(0);
}


for (i = 2; i <= count; i++)
{
if (m % i == 0)
{
printf("%lld 不是一個素數!\n",m);
system("pause");
exit(0);
}
}


printf("%lld 是一個素數!\n",m);
system("pause");
return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章