topic content
Let Pi denote the ith prime number. Given two positive integers M≤N≤104, please output all prime numbers from PM to PN.
Input format:
Input gives M and N on one line, separated by spaces.
Output format:
Output all prime numbers from PM to PN, 1 line for every 10 numbers, separated by spaces, but no extra spaces at the end of the line.
Input sample:
5 27
no blank line at the end
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
no blank line at the end
Problem solving ideas
There is nothing to say about this question. It is enough to be violent according to the title. If the subject of ac can be violent, why not violent? !
Detailed code
#include <iostream>
#include <algorithm>
#include <cmath>
using namespace std;
int s[10001];
bool is_prime(int n){
for(int i = 2;i<=sqrt(n);i++){
if(n%i==0) return false;
}
return true;
}
int main(){
int M,N,count = 1;
cin>>M>>N;
int k = 2;
s[0] = 2;
s[1] = 3;
for( int i = 4;k<10001;i++){
if(is_prime(i)) s[k++] = i;
}
for(int i = M;i<N;i++){
if(count%10==0) cout<<s[i-1];
else cout<<s[i-1]<<" ";
if(count%10==0) cout<<"\n";
count++;
}
cout<<s[N-1];
}