Euclide & non Euclide gcd algorithm
시간 측정 포함
------------------------------------
#include <time.h>
#include <stdio.h>
int gcd1(int m, int n);
int gcd2(int m, int n);
main(void)
{
int m;
int n;
int i;
int result;
double start, finish;
printf("m과 n을 입력하시오(always m>n) : ");
scanf("%d %d", &m, &n);
start = clock();
result = gcd1(m,n);
for(i=0; i<=999999; i++)
finish =clock();
printf("Euclid's result : %d \n", result);
printf("Execution times : %f\n",(finish-start) / (double)CLOCKS_PER_SEC );
start = clock();
result = gcd2(m,n);
for(i=0; i<=999999; i++)
finish =clock();
printf("Euclid's result : %d \n", result);
printf("Execution times : %f\n",(finish-start) / (double)CLOCKS_PER_SEC );
}
int gcd1(int m, int n)//Euclid
{
int r;
int temp;
if(m <n)
{
temp = m;
m = n;
n = temp;
}
while(m%n != 0)
{
r = m % n;
m = n;
n = r;
}
return n;
}
int gcd2(int m, int n)//non
{
int i;
int result=1;
for(i=1; i<=n; i++)
{
if(m%i==0 && n%i==0)
{
result = i;
}
}
return result;
}
'IT > C' 카테고리의 다른 글
Logic and Computer Design Fundamentals 3th Solutions (0) | 2013.01.11 |
---|---|
tiny BASIC interpreter C source - 다차원 배열 추가 (0) | 2013.01.11 |
tiny BASIC interpreter C source (0) | 2013.01.11 |
DFS Traversal Algorithm C source (0) | 2013.01.11 |
Infix -> postfix 변환 C source (0) | 2013.01.11 |