以下是代碼#include
#include
#include
using namespace std;
int main()
{
long a,b,l=0;
cout<<"please input two numbers:\n";
cin>>a>>b;
cout<<"primes from"<
if(a==2)
cout
if(a%2==0)
a++;
for(long m=a;m
{
int sqrtm=sqrt((long double)m);
int i;
for(i=2;i
if(m%i==0)
break;
if(i>sqrtm)
{
if(l++%10==0)
cout<<endl;
cout<<setw(5)<<m;
}
}
}
這個是為了求從A到B的素數(只能被本身和1整除的數)但是輸入1到100 它不會輸出2反而會輸出1.這個怎麼改進? 求各位大師教教,才開始學C++。先在這裡拜謝了。
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
int main()
{
long a, b, l = 0;
cout << "please input two numbers:\n";
cin >> a >> b;
cout << "primes from" << a << " to " << b << endl;
for(long m = a; m < b; m++)
{
int sqrtm = sqrt((long double)m);
int sign = 1;
for(int i = 2; i <= sqrtm; i++)
{
if(m % i == 0)
{
sign = 0;
break;
}
}
if(sign)
{
if(l++%10==0)
cout<<endl;
cout<<setw(5)<<m;
}
}
return 0;
}