So that P i represents the i-th prime number. The current to the two positive integers M ≤ N ≤ . 1 0 . 4 , please output P M to P N all prime numbers.
Input formats:
In the given input row M and N, separated by a space therebetween.
Output formats:
Output from the P M to P N all prime numbers, each representing a digit line 10, separated by spaces therebetween, the end of the line may not have extra space.
Sample input:
5 27
Sample output:
11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 101 103
Idea: I used deposit prime Vector, save enough until the prime number n, the output index to start a prime number m-1 to n, is determined using 6x is prime.
https://blog.csdn.net/huang_miao_xin/article/details/51331710 Why link can judge for ways to explain prime numbers.
code show as below:
#include <iostream> #include <stdlib.h> #include <cstring> #include <math.h> #include <vector> #include <string.h> using namespace std; bool judge(int x) { if (x == 2 || x == 3)return true; if (x % 6 != 1 && x % 6 != 5)return false; int tmp = sqrt(x); for (int i = 5; i <= tmp; i += 6) { if (x%i == 0 || x % (i + 2) == 0)return false; } return true; } int main() { int m,n; scanf("%d %d",&m,&n); int num=2; vector<int> vec; while(vec.size()<n+1) { if(judge(num))vec.push_back(num); num++; } for(int i=m-1,j=0;i<n;i++,j++) { if(j%10==0&&i!=m-1)printf("\n");//换行 if(i!=m-1&&(i-(m-1))%10!=0)printf(" ");//空格 printf("%d",larger]); } Return 0 ; }