//不能被2-sqrt(m)整除的奇数即是素数
#include<iostream>
#include<math.h>
#include<iomanip>
using namespace std;
int main()
{
long m,a,b,temp; //定义变量
int i, l=0;
cout << "输入a,b:"; //输入a,b
cin >> a>>b;
if (a > b) //确定a,b大小
{
temp = a;
a = b;
b = temp;
}
cout << "a-b之间的素数为:";
if (a % 2 == 0) //a是偶数就加1
a++;
for (m = a; m <= b; m += 2) //m每次加2
{
for (i = 2; i <= sqrt(m); i++)
{
if (m % i == 0)
break; //m为非质数,结束内循环,开始判断下一个数
}
if (i > sqrt(m)) //m为质数,十个一行输出
{
if (l++ % 10 == 0)
cout << endl;
cout << setw(5) << m;
}
}
cout << endl;
return 0;
}
if (l++ % 10 == 0)
cout << endl;
cout << setw(5) << m;
注意在此之前的提示行输出时应不换行,因为l刚开始为0,首先换一行