POJ 3518 Prime Gap

Source: Internet
Author: User

Prime Gap
1#include <cmath>2#include <cstdio>3#include <algorithm>4#include <iostream>5#include <string>6#include <cstring>7#include <vector>8 using namespacestd;9 #defineMax 100000Ten intprime[max+5]; One //bool vis[max+5]; A voidGet_prime (intN) { -     //memset (vis,false,sizeof (Vis)); -prime[0]=2; theprime[1]=2; -     intI=3, index=2; -      for(; index<=n;i+=2){ -         //cout<<i<<endl; +         //cout<<prime[index-1]<<endl; -         intHALF=SQRT (i*1.0)+1; +         intj=1; A          while(J<index&&half>prime[j]) {//do not know why J<index can be removed. It's also right to get rid of it.  at             //cout<<i<< "" <<j<< "" <<prime[j]<<endl; -             if(i%prime[j]==0){ -                  Break; -             } -J + +; -         } in         if(half<=Prime[j]) { -prime[index++]=i; to         } +     } -     //cout<<prime[index-1]<<endl; the } * intMain () { $     intN;Panax Notoginseng get_prime (max); -      while(cin>>n&&N) { the         intI=1; +          while(prime[i]<N) { Ai++; the         } +         if(prime[i]==N) { -cout<<0<<Endl; $         } $         Else{ -cout<<prime[i]-prime[i-1]<<Endl; -         } the     } -     return 0;Wuyi}

Time Limit: 5000MS Memory Limit: 65536K
Total Submissions: 8766 Accepted: 5148

Description

The sequence of n−1 consecutive composite numbers (positive integers that is not prime and is equal to 1) lying betwee N-successive prime numbers p and p + n is called a prime gap of length n. For Example,‹24, 25, 26, 27, 28›between + is a prime gap of length 6.

Your mission is to write a program to calculate, for a given positive integer K, the length of the prime gap that Contains K. For convenience, the length was considered 0 in case no prime gap contains K.

Input

The input is a sequence of lines each of which contains a single positive integer. Each positive integer was greater than 1 and less than or equal to the 100000th prime number, which is 1299709. The end of the input is indicated by a line containing a single zero.

Output

The output should is composed of lines each of the which contains a single non-negative integer. It is the length of the prime Gap, the contains the corresponding positive integer in the input if it is a composite numbe R, or 0 otherwise. No Other characters should occur in the output.

Sample Input

10112724921700

Sample Output

4060114

Source

Japan 2007 Analysis:

POJ 3518 Prime Gap

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.