Recommanded Free YOUTUBE Lecture: <% selectedImage[1] %>

#include <stdio.h>
#include <math.h>
#include <time.h>

#define MAX 1000000
int main(void) {
    int i,j;
    int flag;
    int limit;
    int loop_count;
    int count;
    clock_t stime, etime;

    stime = clock();
    flag = 1;
    count = 0;
    loop_count = 0;
    for ( i = 2; i <= MAX; i++ )
    {
      if ( i == 2 || (i & 1) == 1 )
      {
        limit = (int)sqrt(i);
        for ( j = 2; j <= limit; j++ )
        {
          loop_count++;
          if ( i % j == 0 )
          {
            flag = 0;
            break;
          }
        }
        if ( flag != 0 )
        {
          //printf("%d\n",i);
          count++;
        }
        flag = 1;
      }
    }

    etime = clock();
    printf("loop count = %d total prime number = %d\n",loop_count,count);
    printf("Time : %.3fs\n",(double)(etime - stime)/CLOCKS_PER_SEC);
    return 0;
}

테스트 결과
$ ./prime
loop count = 67240405 total prime number = 78498
Time : 1.080s