[Mathematics] GCD LCM

유클리드 호제법을 활용한 최대공약수 최소공배수 산출 공식

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <stdio.h>
int gcd(int p, int q){
if(q==0) return p;
return gcd(q, p%q);
}
int lcm(int p, int q){
return p * q / gcd(p,q);
}
int main(int argc, char** argv){
int A, B;
setbuf(stdout, NULL);
scanf("%d %d",&A,&B);
printf("%d %d\n",gcd(A,B),lcm(A,B));
return 0; //정상종료시 반드시 0을 리턴해야 합니다.
}
Author: Song Hayoung
Link: https://songhayoung.github.io/2020/07/21/Algorithm/gcd-lcm/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.