求質數
(高顯忠, sjgau4311@gmail.com, 2011-11-10 08:45)
1樓
/*
Total= 4, dt= 0.000
Total= 25, dt= 0.000
Total= 168, dt= 0.001
Total= 1229, dt= 0.031
Total= 9592, dt= 2.377
Total= 78498, dt= 198.760
.
.
.
*/
// ----------------------------------------------
#include <cstdlib>
#include <iostream>
using namespace std;
#include "ccc.h"
int is_prime(int no)
{
int i;
// -2, -1, 0, 1, 2, 3
if (no <= 1) return 0;
// no >= 2
if (no == 2) return 1;
for (i=2;i<=(no-1);i++) {
if ((no%i)==0) return 0;
}
return 1;
}// end of is_prime()
// ------------------------------------
int main(int argc, char *argv[])
{
int no, ct, i, t1;
double dt;
for (no=10;no>0;no*= 10) {
ct= 0;
time1(&t1);
for (i=1;i<=no;i++) {
if (is_prime(i)) {
ct++;
}
}
time2(t1, &dt);
printf("Total= %12ld, dt= %10.3lf \n", ct, dt);
}
pause();
return 0;
}// end of main()