#5134: 新手一問


leechen1992 (hazyman)


以下是我的程式碼:

#include<iostream>
#include<math.h>
using namespace std;
int main(){
    int x=0,y;
    while(cin >> x){
    if(x==2||x==3)
    cout << "質數" <<endl;
    y=int(sqrt(x));
    for(int i=2;i<=y;i++)
    if(x%i==0){
    cout << "非質數" <<endl;
    break;}
    else if(i==y)
    cout << "質數" <<endl;
   
}
    return 0;
}

 

我想問的是紅色部分,

為什麼當i=某數根號(sqrt(x))就能判定是質數呢?

不太懂這部分的意思@.o

#5135: Re:新手一問


yuchiao0921 (我的字典沒有放棄)


若n 為非質數,存在一個小於等於根號n 的正整數,整除n