#include <cstdio>
#include <cmath>
#include<iostream>
#include<fstream>
#include<vector>
using namespace std;
unsigned long long ind, n;
vector<int> table(0);
ofstream myfile;
int line = 1;
bool is_prime(unsigned long long n)
{
int m = sqrt(n);
//cout << __LINE__ << ":" << n << ":" << m << endl;
int len = table.size();
for (int i = 0; i <= len; i++)
{
if (table[i] > m)
{
table.push_back((int) n);
myfile << n << ",";
//cout << __LINE__ << "true:" << n << ":" << table[i] << endl;
line++;
if (line >= 10)
{
line = 0;
myfile << "\n";
}
return true;
}
if (n % table[i] == 0)
{
//cout << __LINE__ << "false:" << n << ":" << table[i] << endl;
return false;
}
}
}
int main(int argc , char **argv)
{
myfile.open("example.txt", ios::app);
myfile << 2 << ",";
table.push_back(2);
for (ind = 3; ind < 10000; ind += 2)
{
is_prime(ind);
}
myfile.close();
return 0;
}
程序:
将10000以内的素数,输出到文件example.txt. 10个换行.