请编写一个函数,确定一个数是否为质数。在程序中调用此函数来确定并打印1~10000之间的所有质数。
输出格式:
每数宽度为6,每行输出10个数。
实例输出:
The prime numbers from 1 to 10000 are:
2 3 5 7 11 13 17 19 23 29
31 37 41 43 47 53 59 61 67 71
73 79 83 89 97 101 103 107 109 113
……
9643 9649 9661 9677 9679 9689 9697 9719 9721 9733
9739 9743 9749 9767 9769 9781 9787 9791 9803 9811
9817 9829 9833 9839 9851 9857 9859 9871 9883 9887
9901 9907 9923 9929 9931 9941 9949 9967 9973
There were 1229 prime numbers
参考程序模板:
#include <iostream>
using namespace std;
#include <iomanip>
/* write prototype for function prime */
int main()
{
int count = 0;
cout << "The prime numbers from 1 to 10000 are:\n";
for ( int loop = 2; loop <= 10000; ++loop ) {
if( /* make call to function prime */ ) {
++count;
cout << setw(6) << loop;
if (count % 10 == 0)
cout << '\n';
}
}
cout << '\n' << "There were " << count
<< " prime numbers\n";
return 0;
}
bool prime( int n )
{
for( int i = 2; /* write loop condition */; i++)
if( /* write code to test if n is divisible by i */ )
return false;
return true; //number is prime
}