A prime number is a number which has exactly two distinct divisors: one and itself. For example, numbers 2, 7, 3 are prime, and 1, 6, 4 are not.
The next prime number after x is the smallest prime number greater than x. For example, the next prime number after 2 is 3, and the next prime number after 3 is 5. Note that there is exactly one next prime number after each number. So 5 is not the next prime number for 2.
One cold April morning Panoramix predicted that soon Kakofonix will break free from his straitjacket, and this will be a black day for the residents of the Gallic countryside.
Panoramix's prophecy tells that if some day Asterix and Obelix beat exactly x Roman soldiers, where x is a prime number, and next day they beat exactly y Roman soldiers, where y is the next prime number after x, then it's time to wait for Armageddon, for nothing can shut Kakofonix up while he sings his infernal song.
Yesterday the Gauls beat n Roman soldiers and it turned out that the number n was prime! Today their victims were a troop of m Romans (m > n). Determine whether the Gauls should wait for the black day after today's victory of Asterix and Obelix?
The first and only input line contains two positive integers — n and m (2 ≤ n < m ≤ 50). It is guaranteed that n is prime.
Pretests contain all the cases with restrictions 2 ≤ n < m ≤ 4.
Print YES, if m is the next prime number after n, or NO otherwise.
3 5
YES
7 11
YES
7 9
NO
解题说明:此题就是暴力穷举即可,判断n到m之间的数是否为质数,为了简单,编写一个子函数专门进行判断
#include<cstdio> #include<iostream> #include<cstring> #include<cmath> using namespace std; bool Check(int n) { int i; bool flag; flag=true; for(i=2;i<=n/2;i++) { if(n%i==0) { flag=false; break; } } if(flag==false) { return false; } else { return true; } } int main() { int n,m; int i,j; bool flag; scanf("%d %d",&n,&m); for(i=n+1;i<=m;i++) { flag=Check(i); if(flag==true) { break; } } if(i==m&&flag==true) { printf("YES\n"); } else { printf("NO\n"); } return 0; }