题目描述
令Pi表示第i个素数。现任给两个正整数M <= N <= 10000,请输出PM到PN的所有素数。
输入描述:
输入在一行中给出M和N,其间以空格分隔。
输出描述:
输出从PM到PN的所有素数,每10个数字占1行,其间以空格分隔,但行末不得有多余空格。
输入例子:
5 27
输出例子:
11 13 17 19 23 29 31 37 41 43
47 53 59 61 67 71 73 79 83 89
97 101 103
#include <iostream>
#include<math.h>
using namespace std;
int panduan(int n);
int main(){
int A[10000];
A[0] = 2;
A[1] = 3;
A[2] = 5;
int n = 3;
int M, N;
int flag = 0;
cin >> M >> N;
for (int i = 7;n<=N-1; i = i + 2){
if (panduan(i)){
A[n] = i;
n++;
}
}
for (int i = M-1 ; i <N -1; i++){
cout << A[i];
flag++;
if (flag % 10 == 0)
cout << endl;
else
cout << " ";
}
cout << A[N-1];
}
int panduan(int n){
int flag = 1;
for (int i = 2; i < sqrt(n)+1; i++){
if (n % i == 0){
flag = 0;
break;
}
}
return(flag);
}
#include<iostream>
#include<cmath>
using namespace std;
int isPrime(int n);
int main(){
int prime[10000];
prime[0] = 2;
prime[1] = 3;
prime[2] = 5;
int n = 3;
int M,N;
int flag = 0;
cin >> M >> N;
for(int i = 7;n<=N-1;i = i + 2){
if(isPrime(i)){
prime[n] = i;
n ++;
}
}
for(int i = M-1;i < N-1;i ++){
cout << prime[i];
flag ++;
if(flag % 10 == 0)
cout << endl;
else
cout << " ";
}
cout << prime[N-1];
}
int isPrime(int n){
int flag = 1;
for(int i = 2;i < sqrt(n)+1;i ++){
if(n % i == 0){
flag = 0;
break;
}
}
return(flag);
}