POJ 3518 Prime Gap binary search for nether and prime sieve method

Source: Internet
Author: User

/*
Test instructions: Input has multiple sets of data, each set of data one n, if n is a prime number, output 0 otherwise output from n the last two prime number of the product, the 100,000th prime number is 1299709, all the primes within this range
Thinking: Prime Sieve method plus two points to find the lower bound
*/

#include <stdio.h>

int a[1299720],pri[100005];
int serch (int v)//binary Find Nether
{
int mid,x=0,y=100001;
while (X<y)
{
Mid= (y+x)/2;
if (PRI[MID]&GT;=V)
Y=mid;
Else
x=mid+1;
}
printf ("pri[x]=%d\n", X);
if (PRI[X]==V)
return 0;
Else
return pri[x]-pri[x-1];
}
int main ()
{
int i,j,k=0,n;
for (i=2; i<=1299709; i++)//Sieve method
if (!a[i])
{
Pri[k++]=i;
for (j=i*2; j<=1299709; j+=i)
A[j]=1;
}
while (~SCANF ("%d", &n), N)
{
printf ("%d\n", Serch (n));


}
return 0;
}

POJ 3518 Prime Gap binary search for nether and prime sieve method

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.