查找hcf和lcm的python程序

塔伦·卡雷(Tarun Khare)

我在python中编写了以下progran,以找出两个数字a和b的hcf和lcm。x是两个数字中的较大者,y较小,这两个都是我打算在程序上部找到的。它们将在以后用于查找hcf和lcm。但是当我运行它时,它将x阴影为红色。我不明白原因。

a,b=raw_input("enter two numbers (with space in between: ").split()
if (a>b):
    int x==a
else:
    int x==b
for i in range (1,x):
    if (a%i==0 & b%i==0):
        int hcf=i
print ("hcf of both is: ", hcf)
for j in range (x,a*b):
    if (j%a==0 & j%b==0):
        int lcm=j
print ("lcm of both is: ", lcm)        

查找lcm的这种算法,hcf在c中可以完美运行,所以我不认为algo应该有问题。这可能是一些语法问题。

巴拉比
import sys
a = int(sys.argv[1])
b = int(sys.argv[2])
sa = a
sb = b
r = a % b
while r != 0:
    a, b = b, r
    r = a % b
h = b
l = (sa * sb) / h
print('a={},b={},hcf={},lcm={}\n'.format(sa,sb,h,l))

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章