Skip to content

Instantly share code, notes, and snippets.

@stripe-q
Created Jun 19, 2017
Embed
What would you like to do?
최소공배수와 최대공약수
from lcd import lcd
from functools import reduce
def main():
n = reduce(lcd, range(1, 21))
print(n)
if __name__ == '__main__':
main()
# 두 수를 입력받아 최대공약수를 구한다.
def gcd(a, b):
if a < b:
a, b = b, a
if a % b == 0:
return b
return gcd(b, a % b)
if __name__ == '__main__':
x, y = [int(n) for n in input("두 수를 입력하세요:").split()[:2]]
g = gcd(x, y)
print("%d와 %d의 최대 공약수는 %d 입니다." % (x, y, g))
from gcd import gcd
def lcd(a, b):
g = gcd(a, b)
x, y = a // g, b // g
return x * y * g
if __name__ == '__main__':
x, y = [int(n) for n in input("두 수를 입력하세요:").split()[:2]]
l = lcd(x, y)
print("%d와 %d의 최소 공배수는 %d 입니다." % (x, y, l))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment