This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Source : https://www.math.wustl.edu/~victor/mfmm/compaa/gcd.c | |
*/ | |
#include <stdio.h> | |
/* Standard C Function: Greatest Common Divisor */ | |
int gcd(int a, int b) | |
{ | |
int c; | |
while (a != 0) { | |
c = a; a = b % a; b = c; | |
} | |
return b; | |
} | |
/* Recursive Standard C Function: Greatest Common Divisor */ | |
int gcdr(int a, int b) | |
{ | |
if (a == 0) return b; | |
return gcdr(b%a, a); | |
} | |
/* Recursive one line implementation | |
int gcd(int a,int b) { | |
return a == 0 ? b : gcd(b % a, a); | |
} | |
int gcd(int a, int b) | |
{ | |
return b ? gcd(b, a%b) : a; | |
} | |
*/ | |
int main(void) | |
{ | |
int a, b, c; | |
a = 299792458; | |
b = 6447287; | |
c = 256964964; | |
printf("a=%d, b=%d, c=%d\n", a, b, c); | |
printf("gcd(a,b)=gcd(%d,%d)=%d\n", a, b, gcd(a, b)); | |
printf("gcd(a,b)=gcdr(%d,%d)=%d\n", a, b, gcdr(a, b)); | |
printf("gcd(a,c)=gcd(%d,%d)=%d\n", a, c, gcd(a, c)); | |
printf("gcd(a,c)=gcdr(%d,%d)=%d\n", a, c, gcdr(a, c)); | |
printf("gcd(c,b)=gcd(%d,%d)=%d\n", c, b, gcd(c, b)); | |
printf("gcd(c,b)=gcdr(%d,%d)=%d\n", c, b, gcdr(c, b)); | |
printf("gcd(a,b,c)=gcd(%d,gcd(%d,%d))=%d\n", a, b, c, gcd(a, gcd(b, c))); | |
printf("gcd(a,b,c)=gcdr(%d,gcdr(%d,%d))=%d\n", a, b, c, gcdr(a, gcdr(b, c))); | |
return 0; | |
} |
'알고리즘 & Problem Solving > 레퍼런스코드' 카테고리의 다른 글
[레퍼런스코드] 에라토스테네스의 체 (0) | 2020.05.08 |
---|---|
[레퍼런스코드] Disjoint Set 코드(DSU / Union-Find 자료구조) (0) | 2020.01.25 |
[레퍼런스코드] 머지소트(병합정렬) (0) | 2019.10.30 |