1. 程式人生 > >藍橋杯 ADV-91 演算法提高 素數判斷

藍橋杯 ADV-91 演算法提高 素數判斷

編寫一函式IsPrime,判斷某個大於2的正整數是否為素數。
樣例輸入: 
5
樣例輸出:
yes
樣例輸入: 
9
樣例輸出:
no
注意:是素數輸出yes,不是素數輸出no,其中yes和no均為小寫。

#include <iostream>
using namespace std;
int main() {
    int n;
    cin >> n;
    for(int i = 2; i * i <= n; i++) {
        if(n % i == 0) {
            cout << "no";
            return 0;
        }
    }
    cout << "yes";
    return 0;
}