Problem Description

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

10
11
27
2
492170
0

Sample Output

4
0
6
0
114

题意:两个素数 a、b 之间的区间 (a,b] 称谓非素数区间,给出一个数 n,求 n 所在的非素数区间长度。

思路:先打表求素数,若 n 为素数,则长度为0,若 n 为合数,找出相邻两素数,长度为两素数差

Source Program

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<string>
#include<cstdlib>
#include<queue>
#include<set>
#include<map>
#include<stack>
#include<ctime>
#include<vector>
#define INF 0x3f3f3f3f
#define PI acos(-1.0)
#define N 5000005
#define MOD 1e9+7
#define E 1e-6
#define LL long long
using namespace std;
LL prime[N],cnt;
bool bprime[N];
void make_prime()
{memset(bprime,true,sizeof(bprime));bprime[0]=false;bprime[1]=false;for(LL i=2;i<=N;i++){if(bprime[i]){prime[cnt++]=i;for(LL j=i*i;j<=N;j+=i)bprime[j]=false;}}
}
int main()
{make_prime();LL n;while(scanf("%lld",&n)!=EOF&&n){if(bprime[n])printf("0\n");else{for(LL i=0;i<cnt;i++)if(prime[i]<n&&prime[i+1]>n)printf("%lld\n",prime[i+1]-prime[i]);}}return 0;
}

Prime Gap(POJ-3518)相关推荐

  1. POJ 3518 Prime Gap(素数题)

    [题意简述]:输入一个数,假设这个数是素数就输出0,假设不是素数就输出离它近期的两个素数的差值,叫做Prime Gap. [分析]:这题过得非常险.由于我是打的素数表. 由于最大的素数是1299709 ...

  2. Bailian2734 十进制到八进制【入门】(POJ NOI0113-45)

    问题链接:POJ NOI0113-45十进制到八进制 2734:十进制到八进制 总时间限制: 1000ms 内存限制: 65536kB 描述 把一个十进制正整数转化成八进制. 输入 一行,仅含一个十进 ...

  3. Bailian2676 整数的个数【入门】(POJ NOI0105-11)

    问题链接:POJ NOI0105-11 整数的个数 2676:整数的个数 总时间限制: 1000ms 内存限制: 65536kB 描述 给定k(1 < k < 100)个正整数,其中每个数 ...

  4. Bailian4029 数字反转【进制】(POJ NOI0105-29)

    问题链接:POJ NOI0105-29 数字反转 4029:数字反转 总时间限制: 1000ms 内存限制: 65535kB 描述 给定一个整数,请将该数各个位上数字反转得到一个新数.新数也应满足整数 ...

  5. Bailian2735 八进制到十进制【入门】(POJ NOI0113-46)

    问题链接:POJ NOI0113-46 八进制到十进制 2735:八进制到十进制 总时间限制: 1000ms 内存限制: 65536kB 描述 把一个八进制正整数转化成十进制. 输入 一行,仅含一个八 ...

  6. Silver Cow Party (POJ - 3268 )

    Silver Cow Party (POJ - 3268 ) 这道题是我做的最短路专题里的一道题,但我还没做这个,结果比赛就出了,真是.......... 题目: One cow from each ...

  7. 吴昊品游戏核心算法 Round 7 —— 熄灯游戏AI(有人性的Brute Force)(POJ 2811)

    暴力分为两种,一种属于毫无人性的暴力,一种属于有人性 的暴力.前面一种就不说了,对于后面一种情况,我们可以只对其中的部分问题进行枚举,而通过这些子问题而推导到整个的问题中.我称之为有人性的Brute ...

  8. 【二分】Best Cow Fences(poj 2018)

    Best Cow Fences poj 2018 题目大意: 给出一个正整数数列,要你求平均数最大,长度不小于M的字串,结果乘1000取整 输入样例 10 6 6 4 2 10 3 8 5 9 4 1 ...

  9. 昂贵的聘礼(poj 1062)

    Description 年轻的探险家来到了一个印第安部落里.在那里他和酋长的女儿相爱了,于是便向酋长去求亲.酋长要他用10000个金币作为聘礼才答应把女儿嫁给他.探险家拿不出这么多金币,便请求酋长降低 ...

最新文章

  1. Javascript JSON 序列化和反序列化
  2. f12控制台如何查看consul_如何打印consul的错误信息
  3. rsync和inotify实时同步配置 exclude排除多个文件夹
  4. Queries with streaming sources must be executed with writeStream.start()
  5. linux mysql集群 备份与恢复,Linux下MySQL的备份和恢复
  6. 深度学习2.0-15.随机梯度下降之梯度下降简介
  7. 16:忽略大小写的字符串比较
  8. 程序人生 - 王者荣耀重名代码
  9. 网络编程-在线英英词典项目
  10. CleanMyMac X断网激活码免费共享教程免费分享
  11. java中var是什么意思_Java 10中的var是什么?
  12. 【Ant Design Vue】封装导出Excel文件的功能模块到ele-pro-table
  13. python学习日常-----作业(4)
  14. 【Python】Time模块 ValueError: unconverted data remains: UnicodeEncodeError:
  15. linux连接蓝牙设备,Linux 下连接蓝牙设备
  16. noip 2015 pj 普及组 第四题 推销员 salesman
  17. EMU8086使用及8086指令系统(微机原理实验)
  18. 创意动手做:再也不用数据线,华为D1改装无线充电!
  19. Xposed框架搭建及模块编写相关问题
  20. RabbitMQ快速入门及实例演示

热门文章

  1. java面试题_Java面试题总结(2020年多家公司整理的300道Java面试题手册)
  2. 假如王撕葱是程序员。。。
  3. 100篇架构文章打包,及offer面试题下载
  4. Linux基本操作——Linux磁盘基本概念
  5. 报名 | 美团技术沙龙第64期:美团不同业务场景下的系统架构实践
  6. 支付宝二面微服务、分布式架构?太真实了!
  7. spring + hibernate + mysql 事务不回滚
  8. Linux IPC实践(3) --具名FIFO
  9. Redis-字符串(string)基础
  10. filebeat + es 日志分析