(SPOJ2)Prime Generator

Peter wants to generate some prime numbers for his cryptosystem. Help him! Your task is to generate all prime numbers between two given numbers!

Input

The input begins with the number t of test cases in a single line (t<=10). In each of the next t lines there are two numbers m and n (1 <= m <= n <= 1000000000, n-m<=100000) separated by a space.

Output

For every test case print all prime numbers p such that m <= p <= n, one number per line, test cases separated by an empty line.

Example

Input:
2
1 10
3 5

Output:
2
3
5
7

3
5

Warning: large Input/Output data, be careful with certain languages (though most should be OK if the algorithm is well designed)

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>

using namespace std;

int main()
{
    int t;
    scanf("%d\n", &t);
    while(t--)
    {
        int n,m;
        scanf("%d %d\n", &n, &m);
        int * primes = new int[m-n+1];  
        for(int i=0;i<m-n+1;++i)
        primes[i] = 0;      //循环全部赋值为0 

        for(int p=2;p*p<=m;++p)
        {
            int less = n / p;
            less *= p;    // first number <= N && p divides N

            for(int j=less;j<=m;j+=p) 
             if(j != p && j >= n)
              primes[j - n] = 1;
        }

        for(int i=0;i<m-n+1;++i)
        {
            if(primes[i] == 0 && n+i != 1) //输出除去数字"1" 
            printf("%d\n",n+i);
        }

        if(casen)
          printf("\n");

        delete [] primes;
    }
}

 

转载于:https://www.cnblogs.com/cpoint/archive/2013/04/14/3021253.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值