C++实现费马小定理素数测试

来源:互联网 发布:淘宝发布二手商品 编辑:程序博客网 时间:2024/05/14 10:18
#include<iostream>#include<iomanip>#include<cstdlib>#include<ctime>#include<cmath>using namespace std;long long qmod(int a, int b, int p) {    long long res = 1;    long long term = a%p;    while(b) {        if(b&1){            res = (res*term)%p;        }                term = (term*term)%p;        b >>= 1;    }    return res;}bool is_prime(long long n) {       int i;    for(i = 0; i < 100; ++i) {        if(qmod(1+rand()%(n-1),n-1, n) != 1)            break;    }    if(i < 100)        return false;    else         return true;}int main(void) {    int n;    while(cin >> n) {        if(is_prime(n))            cout << "yes" << endl;        else             cout << "no" << endl;    }    return 0;}

2 0
原创粉丝点击