Prime Generator(spoj)

原题:

Prime Generator

Problem code: PRIME1

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这题太水,我也不知道为什么,ac率会这么低,我用最简单的方法竟然过了,本来以为要用到筛法呢,用兴趣的可以看看晒法。不说了,直接上代码:
 1 #include<iostream>
 2 #include<stdio.h>
 3 #include<cmath>
 4 using namespace std;
 5
 6 int isPrime(int n)
 7 {
 8    if(n<=1)
 9        {
10            return false;
11     }
12    if(n==2)
13        {
14            return true;
15        }
16    else
17        {
18            for(int i=2;    i<=sqrt(n);    i++)
19            {
20                if(n%i==0)
21                   return false;
22            }
23            return true;
24        }
25 }
26
27 int main()
28 {
29     int t;
30     int m,n;
31     scanf("%d",&t);
32     while(t--)
33     {
34         scanf("%d%d",&m,&n);
35         for(int j=m;    j<=n;    j++)
36         {
37             if(isPrime(j))
38             printf("%d\n",j);
39         }
40         printf("\n");
41     }
42     return 0;
43 }
时间: 2024-08-01 19:14:03

Prime Generator(spoj)的相关文章

python--yield and generator(生成器)简述

1.想象一个场景:       设想,我想要100个素数,然后对它们累加求和. 通常的想法是,找一个一次性至少能提供100个素数的工具(函数),让它把这100个素数交给我(用return 一次性返回含100个素数的列表).对于100个素数,这显然是可行的.但是,如果我想要无穷多个素数累加,或者非常非常多素数累加,多到几乎可以消耗掉整个内存空间,这时候找一个工具一次性交付这么多数据就显然非常不合适了. 对于之前的设想,我们还可以有另一种想法,就是找一个能不断生产素数的工具,这个工具并不一次交付10

HDU 1014 Uniform Generator(题解)

Uniform Generator Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 32990    Accepted Submission(s): 13081 Problem Description Computer simulations often require random numbers. One way to generat

MyBatis - Generator(MBG)

前言 MyBatis Generator简称MBG,是一个专门为MyBatis框架使用者定制的代码生成器, 可以快速的根据数据表自动生成Bean对象.Java接口及SqlMapper.xml配置文件. 支持基本的增删改查,以及QBC风格的条件查询,这样能够大大减少我们平时开发的工作量. 但是表连接.存储过程等这些复杂sql的定义需要我们手工编写. 传送门:官方文档 下载 Mybatis Generator最完整配置详解 1 <?xml version="1.0" encoding

Digit Generator(生成元)

For a positive integer N, the digit-sum of N is defined as the sum of N itself and its digits. When M is the digitsum of N, we call N a generator of M. For example, the digit-sum of 245 is 256 (= 245 + 2 + 4 + 5). Therefore, 245 is a generator of 256

HDU - 1973 - Prime Path (BFS)

Prime Path Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 987    Accepted Submission(s): 635 Problem Description The ministers of the cabinet were quite upset by the message from the Chief of S

0x31 prime distance(质数)

题目描述: 给定两个整数L和U,你需要在闭区间[L,U]内找到距离最接近的两个相邻质数C1和C2(即C2-C1是最小的),如果存在相同距离的其他相邻质数对,则输出第一对. 同时,你还需要找到距离最远的两个相邻质数D1和D2(即D1-D2是最大的),如果存在相同距离的其他相邻质数对,则输出第一对. 输入格式: 每行输入两个整数L和U,其中L和U的差值不会超过1000000. 输出格式: 对于每个L和U ,输出一个结果,结果占一行. 结果包括距离最近的相邻质数对和距离最远的相邻质数对.(具体格式参照

Life, the Universe, and Everything(spoj)

原题:Your program is to use the brute-force approach in order to find the Answer to Life, the Universe, and Everything. More precisely... rewrite small numbers from input to output. Stop processing input after reading in the number 42. All numbers at i

USACO 1.3 Prime Cryptarithm (枚举)

描述 下面是一个乘法竖式,如果用我们给定的那n个数字来取代*,可以使式子成立的话,我们就叫这个式子牛式. *** x ** ---------- *** *** ---------- **** 数字只能取代*,当然第一位不能为0,况且给定的数字里不包括0. 注意一下在美国的学校中教的"部分乘积",第一部分乘积是第二个数的个位和第一个数的积,第二部分乘积是第二个数的十位和第一个数的乘积. 写一个程序找出所有的牛式. [编辑]格式 PROGRAM NAME: crypt1 INPUT FO

ES6中的迭代器(Iterator)和生成器(Generator)(一)

用循环语句迭代数据时,必须要初始化一个变量来记录每一次迭代在数据集合中的位置,而在许多编程语言中,已经开始通过程序化的方式用迭代器对象返回迭代过程中集合的每一个元素 迭代器的使用可以极大地简化数据操作,于是ES6也向JS中添加了这个迭代器特性.新的数组方法和新的集合类型(如Set集合与Map集合)都依赖迭代器的实现,这个新特性对于高效的数据处理而言是不可或缺的,在语言的其他特性中也都有迭代器的身影:新的for-of循环.展开运算符(...),甚至连异步编程都可以使用迭代器 一.引入 下面是一段标