程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

(daily practice Python) find the maximum common divisor and minimum common multiple of two given positive integers

編輯:Python

Find the maximum common divisor and minimum common multiple of two given positive integers

This problem requires the greatest common divisor and the least common multiple of two given positive integers . Input format :
Enter positive integers in each of the two lines x and y.
Output format :
Output the values of the maximum common divisor and the minimum common multiple in one line .

 sample input 1:
Here's a set of inputs .
for example :

100
1520

sample output 1:
Here is the corresponding output .
for example :

20 7600

The following program realizes this function :

def hcf(x, y):
if x > y:
smaller = y
else:
smaller = x
for i in range(1,smaller + 1):
if((x % i == 0) and (y % i == 0)):
hcf = i
return hcf
def lcm(x, y):
if x > y:
greater = x
else:
greater = y
while(True):
if((greater % x == 0) and (greater % y == 0)):
lcm = greater
break
greater += 1
return lcm
num1 = int(input(" Enter the first number : "))
num2 = int(input(" Enter the second number : "))
print(" The greatest common divisor is ",hcf(num1, num2)," The least common multiple is ",lcm(num1,num2))

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved