python 查找 hcf 和 lcm 的程序
python program to find hcf and lcm
我在python中写了下面的程序来找出两个数a和b的hcf和lcm。 x 是两个数字中较大的一个,y 较小,我打算在程序的上半部分找到这两个数字。它们稍后将用于查找 hcf 和 lcm.but 当我 运行 它时,它将 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 中运行得很好,所以我觉得算法应该没有问题。这可能是一些语法问题。
您几乎是正确的,但是您需要解决一些 Python 语法问题:
a, b = raw_input("enter two numbers (with space in between: ").split()
a = int(a) # Convert from strings to integers
b = int(b)
if a > b:
x = a
else:
x = b
for i in range(1, x):
if a % i == 0 and b % i==0:
hcf = i
print "hcf of both is: ", hcf
for j in range(x, a * b):
if j % a == 0 and j % b == 0:
lcm = j
break # stop as soon as a match is found
print "lcm of both is: ", lcm
使用 Python 2.7.6
测试
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))
a, b = raw_input("enter two numbers (with space in between: ").split()
a = int(a) # Convert from strings to integers
b = int(b)
if a > b:
x = a
else:
x = b
for i in range(1, x + 1):
if a % i == 0 and b % i == 0:
hcf = i
print "hcf of both is:", hcf
for j in range(x, a * b + 1):
if j % a == 0 and j % b == 0:
lcm = j
break # stop as soon as a match is found
print "lcm of both is:", lcm
寻找LCM和HCF的程序
a=int(input("Enter the value of a:"))
b=int(input("Enter the value of b:"))
if(a>b):
x=a
else:
x=b
for i in range(1,x+1):
if(a%i==0)and(b%i==0):
hcf=i
print("The HCF of {0} and {1} is={2}".format(a,b,hcf));
for j in range(x,a*b):
if(j%a==0)and(j%b==0):
lcm=j
break
print("The LCM of {0} and {1} is={2}".format(a,b,lcm));
我在python中写了下面的程序来找出两个数a和b的hcf和lcm。 x 是两个数字中较大的一个,y 较小,我打算在程序的上半部分找到这两个数字。它们稍后将用于查找 hcf 和 lcm.but 当我 运行 它时,它将 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 中运行得很好,所以我觉得算法应该没有问题。这可能是一些语法问题。
您几乎是正确的,但是您需要解决一些 Python 语法问题:
a, b = raw_input("enter two numbers (with space in between: ").split()
a = int(a) # Convert from strings to integers
b = int(b)
if a > b:
x = a
else:
x = b
for i in range(1, x):
if a % i == 0 and b % i==0:
hcf = i
print "hcf of both is: ", hcf
for j in range(x, a * b):
if j % a == 0 and j % b == 0:
lcm = j
break # stop as soon as a match is found
print "lcm of both is: ", lcm
使用 Python 2.7.6
测试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))
a, b = raw_input("enter two numbers (with space in between: ").split()
a = int(a) # Convert from strings to integers
b = int(b)
if a > b:
x = a
else:
x = b
for i in range(1, x + 1):
if a % i == 0 and b % i == 0:
hcf = i
print "hcf of both is:", hcf
for j in range(x, a * b + 1):
if j % a == 0 and j % b == 0:
lcm = j
break # stop as soon as a match is found
print "lcm of both is:", lcm
寻找LCM和HCF的程序
a=int(input("Enter the value of a:"))
b=int(input("Enter the value of b:"))
if(a>b):
x=a
else:
x=b
for i in range(1,x+1):
if(a%i==0)and(b%i==0):
hcf=i
print("The HCF of {0} and {1} is={2}".format(a,b,hcf));
for j in range(x,a*b):
if(j%a==0)and(j%b==0):
lcm=j
break
print("The LCM of {0} and {1} is={2}".format(a,b,lcm));