Liste des Groupes | Revenir à cl c |
#include <time.h>
#include <stdio.h>
#define RUNS 1000
#define SIZE 1000000
int mark[SIZE];
int main(void)
{
time_t start, finish;
int i, loop, n, num;
time(&start);
/* This loop finds the prime numbers between 2 and SIZE */
for (loop = 0; loop < RUNS; ++loop)
{
for (n = 0; n < SIZE; ++n)
mark[n] = 0;
/* This loops marks all the composite numbers with -1 */
for (num = 0, n = 2; n < SIZE; ++n)
if (!mark[n])
{
for (i = 2 * n; i < SIZE; i += n)
mark[i] = -1;
++num;
}
}
time(&finish);
printf("Program takes an average of %f seconds "
"to find %d primes.\n",
difftime(finish, start) / RUNS, num);
}
/*
The result on my slow machine:
Program takes an average of 0.018000 seconds to find 78498 primes.
*/
Les messages affichés proviennent d'usenet.