sqrt¸¦ ÀÌ¿ëÇÑ ¼Ò¼ö±¸Çϱâ
ÃÑ ÆäÀÌÁö ¼ö : 3224

Àüü ÇÔ¼ö/¿ë¾î»çÀü
Facebook Joinc ±×·ì   Joinc QA »çÀÌÆ®



joinc´Â Firefox¿Í chrome¿¡¼­ Å×½ºÆ® Çß½À´Ï´Ù. IE¿¡¼­´Â Å×À̺íÀÌ ±úÁö°Å³ª À̹ÌÁö°¡ º¸ÀÌÁö ¾ÊÀ» ¼ö ÀÖ½À´Ï´Ù. ƯÈ÷ ±¸±Û DocsÀ̹ÌÁöÀÇ °æ¿ì ¿¢¹Úó¸®µÉ ¼ö ÀÖ½À´Ï´Ù.

#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 
 
EmailÀ» ±âÀÔÇϸé, ´ñ±ÛÀÌ ¸ÞÀÏ·Î Àü´ÞµË´Ï´Ù.