杭电ACM2012:素数判定
2017-04-12 21:04
295 查看
#include <iostream> #include <cmath> using namespace std; int isPrime(const int a){ int flag = 1; int b = a; for (int i = 2; i < sqrt(b); i++){ if (b%i == 0) flag=0; } return flag; } int main(){ int n,x,y,s=0; while ((cin >> x >> y) && (x != 0 || y != 0)) { if (x > y){ x = x + y; y = x - y; x = x - y; } for (int n = x; n <= y; n++){ int m = n*n + n + 41; if (isPrime(m)) s++; else break; } if (s == y - x + 1) cout << "OK" << endl; else cout << "Sorry" << endl; s = 0; } return 0; }