判断素数

#include <iostream>
#include <cmath>

using namespace std;

bool IsDivides (const int &a, const int &b)
{
    return (b % a == 0);
}

int FindDivisor (const int &n, int &&test_divisor)
{
    if (pow(test_divisor, 2) > n) {
        return n;
    }

    if (IsDivides (test_divisor, n)) {
        return test_divisor;
    }
    else {
        test_divisor += 2;
        return FindDivisor (n, move(test_divisor));
    }
}

int SmallestDivisor (const int &n)
{
    if (IsDivides (2, n)) {
        return 2;
    }
    return FindDivisor (n, 3);
}

bool IsPrimer (const int &n)
{
    return (n == SmallestDivisor(n));
}

int main ()
{
    cout << SmallestDivisor(19999);
    cout << endl;
    return 0;
}

 

posted @ 2015-02-03 10:32  wu_overflow  阅读(155)  评论(0)    收藏  举报