Prime number Determination

Source: Internet
Author: User

Time limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others)
Total submission (s): 91615 Accepted Submission (s): 32165


Problem description for an expression n^2+n+41, when n takes an integer value in the (x, y) range (including x, y) ( -39<=x<y<=50), the value of the expression is determined to be a prime number.


Input data has multiple groups, one row per group, consisting of two integers, x, y, and when x=0,y=0, indicates that the input ends and the row is not processed.


Output for each given range of values, if the value of the expression is a prime number, outputs "OK", otherwise output "Sorry", each set of output in one row.


Sample Input
0 10 0


Sample Output
Ok

#include <stdio.h>
#include <math.h>
int f (int s)
{
 int i,j=0,k=0;
 //printf ("%d\n", s);
 for (i=2;i<=sqrt (s); i++)
 if (s%i==0)
 j++;
 //else k++;
 if (j==0)
 return 1;
 else
 return 0;
}
Main ()
{
 int x,y,i,j,a,b,c,k;
while (scanf ("%d%d", &x,&y)!=eof)
  {
   if (x==0&&y==0)
   break;
 j=0;
 k=0;
 c=y-x+1;
// printf ("%d\n", c);
 for (i=x;i<=y;i++)
 {
 a=i*i+i+41;
 b=f (a);
 //printf ("%d", b);
 if (b==0)
 k++;
 //{
 // printf ("sorry\n");
 // break;
 //}
 
  if (b==1)
   j + +;
 }
// printf ("%d%d\n", j,k);
 if (j==c)
 printf ("ok\n");
 else printf ("sorry\n");
 }
 return 0; 
}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Prime number Determination

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.