首页 > 代码库 > Prime Generator(spoj)
Prime Generator(spoj)
原题:
Prime GeneratorProblem 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:21 103 5Output:235735
这题太水,我也不知道为什么,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 }
Prime Generator(spoj)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。