The sequence of n − 1 consecutive composite numbers (positive integers that are not prime and not equal to 1) lying between two successive prime numbers p and p + n is called a prime gap of length n. For example, ‹24, 25, 26, 27, 28› between 23 and 29 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 is 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 is 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 be composed of lines each of which contains a single non-negative integer. It is the length of the prime gap that contains the corresponding positive integer in the input if it is a composite number, or 0 otherwise. No other characters should occur in the output.

Sample Input

  1. 10
  2. 11
  3. 27
  4. 2
  5. 492170
  6. 0

Sample Output

  1. 4
  2. 0
  3. 6
  4. 0
  5. 114

给个x,如果x夹在两个质数a,b之间,求b-a,否则输出0

在筛法的时候预处理下距离就好

  1. #include<cstdio>
  2. #include<iostream>
  3. #include<cstring>
  4. #define LL long long
  5. using namespace std;
  6. inline LL read()
  7. {
  8. LL x=,f=;char ch=getchar();
  9. while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
  10. while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
  11. return x*f;
  12. }
  13. LL n;
  14. bool mk[];
  15. int p[],len;
  16. int ans[];
  17. inline LL LLabs(LL a){return a<?-a:a;}
  18. inline void getp()
  19. {
  20. memset(ans,-,sizeof(ans));
  21. for (int i=;i<=;i++)
  22. {
  23. if (!mk[i])
  24. {
  25. p[++len]=i;
  26. ans[i]=;
  27. for (int j=*i;j<=;j+=i)mk[j]=;
  28. }else ans[i]=ans[i-]+;
  29. }
  30. for (int i=;i>=;i--)
  31. {
  32. if (!ans[i])continue;
  33. ans[i]=max(ans[i],ans[i+]);
  34. }
  35. }
  36. int main()
  37. {
  38. getp();
  39. ans[]=-;
  40. while (~scanf("%lld",&n)&&n)printf("%d\n",ans[n]?ans[n]+:);
  41. }

poj 3518

[暑假集训--数论]poj3518 Prime Gap的更多相关文章

  1. [暑假集训--数论]poj1365 Prime Land

    Everybody in the Prime Land is using a prime base number system. In this system, each positive integ ...

  2. [暑假集训--数论]poj1595 Prime Cuts

    A prime number is a counting number (1, 2, 3, ...) that is evenly divisible only by 1 and itself. In ...

  3. [暑假集训--数论]hdu2136 Largest prime factor

    Everybody knows any number can be combined by the prime number. Now, your task is telling me what po ...

  4. [暑假集训--数论]poj2262 Goldbach's Conjecture

    In 1742, Christian Goldbach, a German amateur mathematician, sent a letter to Leonhard Euler in whic ...

  5. [暑假集训--数论]poj2909 Goldbach's Conjecture

    For any even number n greater than or equal to 4, there exists at least one pair of prime numbers p1 ...

  6. [暑假集训--数论]poj2773 Happy 2006

    Two positive integers are said to be relatively prime to each other if the Great Common Divisor (GCD ...

  7. [暑假集训--数论]hdu1019 Least Common Multiple

    The least common multiple (LCM) of a set of positive integers is the smallest positive integer which ...

  8. [暑假集训--数论]poj2115 C Looooops

    A Compiler Mystery: We are given a C-language style for loop of type for (variable = A; variable != ...

  9. [暑假集训--数论]poj2034 Anti-prime Sequences

    Given a sequence of consecutive integers n,n+1,n+2,...,m, an anti-prime sequence is a rearrangement ...

随机推荐

  1. I am too vegetable to all kill the 51nod problems on level 2 and 3.

    51nod level 2:50/51 剩的一个题是切比雪夫距离转曼哈顿距离,现学的,bzoj3710过了,51nod上全wa了,很迷,可能有坑⑧. level 3:62/68 之前有的题有思路但是不 ...

  2. 1143: [CTSC2008]祭祀river

    Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 4018  Solved: 2048[Submit][Status][Discuss] Descript ...

  3. Java 数值计算精度问题

    最近刚好做到涉及金额方面的项目,不像普通in,double,floatt类型来修饰,而是用BigDecimal来修饰,就去收集下了这方面的资料,整理如下: 1.float和double只能用来做科学计 ...

  4. 16.1-Jenkins持续集成01—Jenkins服务搭建和部署

    分类: Linux架构篇   一.介绍Jenkins 1.Jenkins概念 Jenkins是一个功能强大的应用程序,允许持续集成和持续交付项目,无论用的是什么平台.这是一个免费的源代码,可以处理任何 ...

  5. 三十、MySQL 处理重复数据

    MySQL 处理重复数据 有些 MySQL 数据表中可能存在重复的记录,有些情况我们允许重复数据的存在,但有时候我们也需要删除这些重复的数据. 本章节我们将为大家介绍如何防止数据表出现重复数据及如何删 ...

  6. 数据结构-二分查找(Binary Search)

    #include <stdio.h> #include <string.h> #include <stdlib.h> #define LIST_INIT_SIZE ...

  7. psutil——获取系统信息的Python第三方模块

    本文摘自廖雪峰大神个人网站:https://www.liaoxuefeng.com/wiki/1016959663602400/1183565811281984 用Python来编写脚本简化日常的运维 ...

  8. HDU:3336-Count the string(next数组理解)

    Count the string Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Pr ...

  9. 网络流 EK算法模板。

    这篇博客讲得很好 #include<queue> #include<stdio.h> #include<string.h> using namespace std; ...

  10. Spring.Net初认识——竹子整理

    留个脚印,过两天总结. 看到知乎上有人对于DI|IOC 的解释,满不错,收藏下先 作者:OneNoodle链接:http://www.zhihu.com/question/23277575/answe ...