2017年12月23日 星期六

a043: 最大公因數

a043: 最大公因數
'''
如果一個正整數 A 可以被另一個正整數 B 整除,我們稱 B 為 A 的因數,
例如 6 的因數有 1、2、3、6 等四個,
而 9 的因數有 1、3、9 等三個,
其中 1、3 為兩者公共的因數,我們稱之為「公因數」,
其中最大的是 3,我們稱之為「最大公因數」。

輸入說明:
輸入兩個正整數 A、B。
輸出說明:
請輸出 A、B 的最大公因數。

範例輸入: 
輸入1:
6 9

輸入2:
20 12
範例輸出 :

輸出1:
3

輸出2:
4

'''
def gcd(a,b):
    """Compute the greatest common divisor of a and b"""
    while b > 0:
        a, b = b, a % b
    return a
    
def lcm(a, b):
    """Compute the lowest common multiple of a and b"""
    return a * b / gcd(a, b)

print("輸入兩個正整數 A、B。 請輸出 A、B 的最大公因數。")
print(" A、B 需以逗號隔開 > ",end="")
a, b  = map(int,input().split(','))

print(a,b,"最大公因數=",gcd(a,b))
print(a,b,"最小公倍數=",int(lcm(a,b)))


======= ==== RESTART: F:/Python_APSC/a043.py ======================
輸入兩個正整數 A、B。 請輸出 A、B 的最大公因數。
 A、B 需以逗號隔開 > 12,20
12 20 最大公因數= 4
12 20 最小公倍數= 60
>>> 
====== ====== RESTART: F:/Python_APSC/a043.py ======================
輸入兩個正整數 A、B。 請輸出 A、B 的最大公因數。
 A、B 需以逗號隔開 > 13,15
13 15 最大公因數= 1
13 15 最小公倍數= 195
>>> 

沒有留言:

張貼留言

2024年4月24日 星期三 Node-Red Dashboard UI Template + AngularJS 參考 AngularJS教學 --2

 2024年4月24日 星期三 Node-Red Dashboard UI Template + AngularJS 參考 AngularJS教學 --2 AngularJS 實例 <!DOCTYPE html> <html> <head> &...