hdu 2012 素数判定

素数判定

 

题目分析:

构造一个判断素数的函数fun,利用fun函数判断区间内的素数。

代码:

#include <iostream>
#include <math.h>

#define fun(n) n*n + n + 41
using namespace std;
int prime(int n)
{
    if (n % 2 == 0)
        return 1;

    int end = sqrt(n), i;
    for (i = 3; i <= end; i += 2) {
        if (n % i == 0)
            break;
    }

    return i > end ? 0 : 1;
}

int main(void)
{
    int x, y, i;

    while (cin>>x>>y) {
        if (x == 0 && y == 0)
            break;

        for (i = x; i <= y; i++) {
            if (prime(fun(i)))
                break;
        }

        if (i > y)
            printf("OK
");
        else
            printf("Sorry
");
    }

    return 0;
}
原文地址:https://www.cnblogs.com/pcdl/p/12275366.html