POJ3518_Prime Gap【素数】【水题】

Prime Gap

Time Limit: 5000MSMemory Limit: 65536K

Total Submissions: 8499Accepted: 4983

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之间的区间称为非素数区间(包括后边的素数b)。

给你一个数N,求N所在非素数区间的长度。

如素数 23~29  之间的非素数区间为24 25 26 27 28 +素数29。非素数区间长度为6(5+1)

给你一个数25 则25所在的非素数区间长度就为6

思路:

用筛法求素数打表,用PrimeNum存放所有素数。

若N为素数,则输出长度为0

若N为合数,则找出相邻的两个素数,输出长度为两素数的差

#include<stdio.h>

int Prime[1300000],PrimeNum[100010];

int IsPrime()
{
    for(int i = 2; i <= 1300000; i++)
        Prime[i] = 1;
    for(int i = 2; i <= 1300000; i++)
    {
        for(int j = i+i; j <= 1300000; j+=i)
            Prime[j] = 0;
    }
    int num = 0;
    for(int i = 2; i <= 1300000; i++)
    {
        if(Prime[i])
            PrimeNum[num++] = i;
    }
    return num;
}

int main()
{
    int n;
    int num = IsPrime();
    while(~scanf("%d",&n) && n)
    {
        if(Prime[n])
        {
            printf("0\n");
            continue;
        }
        else
        {
            for(int i = 0; i < num; i++)
            {
                if(PrimeNum[i] < n && PrimeNum[i+1] > n)
                    printf("%d\n",PrimeNum[i+1]-PrimeNum[i]);
            }
        }
    }

    return 0;
}
时间: 2024-08-01 20:43:06

POJ3518_Prime Gap【素数】【水题】的相关文章

【UVA - 1644】Prime Gap(水题)

Prime Gap 这里直接写中文了 Descriptions: 对于一个数n,若n为素数则输出0,否则找到距离n最小的两个素数,一个大于n,一个小于n,输出他们的差(正数) Input 多组输入 每行包含一个数n 若n为0,程序结束 Output 对于每个测试数据,输出一个答案占一行 Sample Input 10 11 27 2 492170 0 Sample Output 4 0 6 0 114 题目链接: https://vjudge.net/problem/UVA-1644 水题,先求

HDU ACM 2521 反素数 水题+因子打表

分析:水题,不解释. #include<iostream> using namespace std; int cnt[6000]; void init() //打表 { int i,j; memset(cnt,0,sizeof(cnt)); cnt[1]=1; //1只有他本身 for(i=2;i<=5005;i++) { cnt[i]+=2; //1和他本身 for(j=2;j<=i/2;j++) if(i%j==0) cnt[i]++; } } int main() { int

素数三元组(南阳oj1156)(素数水题)

素数三元组 时间限制:1000 ms  |  内存限制:65535 KB 难度:1 描述 相邻三个奇数都是素数是一种非常少见的情形,也就是三个奇数p-2, p, p+2都是素数,这样就形成了一个素数三元组.请找出三个数都不超过n的所有这样的素数三元组. 输入 输入多组数据,每组测试数据为一个正整数n,n <= 5000000. 输出 输出大小不超过n的所有的素数三元组,每行按照从小到大的顺序输出一个三元组中的三个数,两个数之间用空格间隔.如果不存在这样的素数三元组,请输出"No tripl

POJ2262_Goldbach&#39;s Conjecture【素数判断】【水题】

Goldbach's Conjecture Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 38024 Accepted: 14624 Description In 1742, Christian Goldbach, a German amateur mathematician, sent a letter to Leonhard Euler in which he made the following conjecture:

POJ2909_Goldbach&#39;s Conjecture【素数判断】【水题】

Goldbach's Conjecture Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10116 Accepted: 5973 Description For any even number n greater than or equal to 4, there exists at least one pair of prime numbers p1 and p2 such that n = p1 + p2 This c

蓝桥杯 算法训练 Torry的困惑(基本型)(水题,筛法求素数)

算法训练 Torry的困惑(基本型) 时间限制:1.0s   内存限制:512.0MB 问题描述 Torry从小喜爱数学.一天,老师告诉他,像2.3.5.7--这样的数叫做质数.Torry突然想到一个问题,前10.100.1000.10000--个质数的乘积是多少呢?他把这个问题告诉老师.老师愣住了,一时回答不出来.于是Torry求助于会编程的你,请你算出前n个质数的乘积.不过,考虑到你才接触编程不久,Torry只要你算出这个数模上50000的值. 输入格式 仅包含一个正整数n,其中n<=100

POJ 2739 Sum of Consecutive Prime Numbers(素数打表水题)

[题意简述]:题意很简单,就是用连续的素数加和,计算有多少个这样的连续的素数数列可以使这个序列的和等于输入的数. [分析]:很经典的素数模板,基本所有有关素数的题,我都会使用这个模板. // 268K 16Ms #include<iostream> using namespace std; #define N 10000 bool isprime[N]; long long prime[1300],nprime; // 注意long long void doprime() { long lon

POJ百道水题列表

以下是poj百道水题,新手可以考虑从这里刷起 搜索1002 Fire Net1004 Anagrams by Stack1005 Jugs1008 Gnome Tetravex1091 Knight Moves1101 Gamblers1204 Additive equations 1221 Risk1230 Legendary Pokemon1249 Pushing Boxes 1364 Machine Schedule1368 BOAT1406 Jungle Roads1411 Annive

JAVA 水题

纯粹是让我来掌握熟练度的. 1.金蝉素数 某古寺的一块石碑上依稀刻有一些神秘的自然数. 专家研究发现:这些数是由1,3,5,7,9 这5 个奇数字排列组成的5 位素数,且同时去掉它的最高位与最低位数字后的三位数还是素数,同时去掉它的高二位与低二位数字后的一位数还是素数.因此,人们把这些神秘的素数称为金蝉素数,喻意金蝉脱壳之后仍为美丽的金蝉. 输出所有金蝉素数. ANSWER: 13597 53791 79531 91573 95713 package test; import java.util

【转载】POJ水题大集合

POJ水题大集合 poj1000:A+B problempoj1002:电话上按键对应着数字.现在给n个电话,求排序.相同的归一类poj1003:求最小的n让1+1/2+1/3+...+1/n大于给的一个实数poj1004:求一堆实数的平均数poj1005:由坐标 (0,0) 开始,以半圆为形状每年侵蚀50m^2,问(0,0)开始到(x,y)结束需要多长时间poj1006:三个周期是常数.现在给三个周期出现高峰的时候,问下一次出现高峰是什么时候poj1007:求字符串排序poj1008:一种日历