Python 查找最多5个数字的LCM
我正试图写一个程序来计算五个数字的LCM。 到目前为止,我已经编写了以下代码:Python 查找最多5个数字的LCM,python,list,lcm,Python,List,Lcm,我正试图写一个程序来计算五个数字的LCM。 到目前为止,我已经编写了以下代码: a = b = True while a == True: x = input('\nEnter two to five numbers separated by commas: ').split(',') if x.index(0) == True: print('\nNo zeroes.') if len(x) < 2: while b == True
a = b = True
while a == True:
x = input('\nEnter two to five numbers separated by commas: ').split(',')
if x.index(0) == True:
print('\nNo zeroes.')
if len(x) < 2:
while b == True:
x.append(input('\nTwo numbers are needed. Enter one more: '))
b = False
if len(x) > 5:
print('\nDon\'t enter more than five numbers.')
continue
a = False
当我尝试运行代码时,出现以下错误:
Enter two to five numbers separated by commas: 0
Traceback (most recent call last):
File "/home/mint/.config/spyder-py3/lcm.py", line 4, in <module>
if x.index(0) == True:
ValueError: 0 is not in list
这怎么可能?我的输入值为0。可能类似于此,可能适合您的目的:
from functools import reduce
from math import gcd
x = []
while len(x) < 2 and len(x) <= 5: # Using this condition removes unneccesary break code:
x = [int(i) for i in input('\nEnter two to five numbers separated by commas: ').split(',')]
if x[0] == 0:
print('\nNo zeroes.')
if len(x) > 5:
print('\nDon\'t enter more than five numbers.')
if len(x) < 2:
x.append(int(input('\nTwo numbers are needed. Enter one more: ')))
lcm = reduce(lambda x, y: x * y // gcd(x, y), x) # lcm = x * y / gcd(x, y)
print(lcm)
也许像这样的东西,可能适合你的目的:
from functools import reduce
from math import gcd
x = []
while len(x) < 2 and len(x) <= 5: # Using this condition removes unneccesary break code:
x = [int(i) for i in input('\nEnter two to five numbers separated by commas: ').split(',')]
if x[0] == 0:
print('\nNo zeroes.')
if len(x) > 5:
print('\nDon\'t enter more than five numbers.')
if len(x) < 2:
x.append(int(input('\nTwo numbers are needed. Enter one more: ')))
lcm = reduce(lambda x, y: x * y // gcd(x, y), x) # lcm = x * y / gcd(x, y)
print(lcm)
因为x是字符串列表,而不是整数。要进行检查,可以将x转换为整数列表
因为x是字符串列表,而不是整数。要进行检查,可以将x转换为整数列表
您可以先进行素数分解,然后计算LSM:
from collections import Counter, defaultdict
from functools import reduce
def prime_factors(n):
primes = []
i = 2
while n > 1:
while n % i == 0:
n = n / i
primes.append(i)
i = i + 1
if primes:
return primes
return [n] # if n is a prime
def lcm(my_list):
f = [Counter(prime_factors(e)) for e in my_list]
lcm_primes = Counter()
for d in f:
for prime, count in d.items():
lcm_primes[prime] = max((lcm_primes[prime], count))
return reduce(lambda x, y: x * (y[0] ** y[1]), lcm_primes.items(), 1)
lcm([3, 15, 9, 6, 2])
输出:
90
您可以先进行素数分解,然后计算LSM:
from collections import Counter, defaultdict
from functools import reduce
def prime_factors(n):
primes = []
i = 2
while n > 1:
while n % i == 0:
n = n / i
primes.append(i)
i = i + 1
if primes:
return primes
return [n] # if n is a prime
def lcm(my_list):
f = [Counter(prime_factors(e)) for e in my_list]
lcm_primes = Counter()
for d in f:
for prime, count in d.items():
lcm_primes[prime] = max((lcm_primes[prime], count))
return reduce(lambda x, y: x * (y[0] ** y[1]), lcm_primes.items(), 1)
lcm([3, 15, 9, 6, 2])
输出:
90
您的输入是一个字符串。您需要将拆分的元素转换为整数。e、 g.x=[inti代表输入'\n输入两到五个以逗号分隔的数字:'.split',',],因为x是字符串列表,而不是整数。我还没有检查过,但它可能会起作用。索引“0”@你太棒了,工作起来很有魅力。非常感谢。但是,我用这个到底完成了什么?我对Python和编程都是新手,请原谅我的无知。编辑:当我输入任何其他数字时,它将不起作用。请澄清,如果x.index0==True,您是否要检查x中的数字0?@dspencer我按照您的建议做了,但没有看到不同的结果。您的输入是一个字符串。您需要将拆分的元素转换为整数。e、 g.x=[inti代表输入'\n输入两到五个以逗号分隔的数字:'.split',',],因为x是字符串列表,而不是整数。我还没有检查过,但它可能会起作用。索引“0”@你太棒了,工作起来很有魅力。非常感谢。但是,我用这个到底完成了什么?我对Python和编程都是新手,请原谅我的无知。编辑:当我输入任何其他数字时,它将不起作用。只是要澄清一下,如果x.index0==真,你想检查x中的数字0吗?@dspencer我按照你的建议做了,但没有看到不同的结果。是的,它工作得很好。非常感谢。我想写代码而不使用任何来自导入的东西。这是我正在学习的一本书中的一个练习题。如果你为我这样做,那就违背了学习这门语言的目的。不过,我非常感谢您的提议。@Kos如果您想看一看,这里是reduce的实现和gcd@Kos的示例实现,lambda函数为两个数字托管lcm,reduce使用前面的计算结果对每对数字进行应用,正如在Yup中看到的那样,效果非常好。非常感谢。我想写代码而不使用任何来自导入的东西。这是我正在学习的一本书中的一个练习题。如果你为我这样做,那就违背了学习这门语言的目的。不过,我非常感谢您的提议。@Kos如果您想看一看,这里是reduce的实现和gcd@Kos的示例实现,lambda函数为两个数字托管lcm,reduce使用前面的计算结果将其应用于每对数字,如感谢中所示。现在我怎么能对任何负数做同样的事情呢?@Kos我刚刚更新了我的答案。这是你想要的吗?谢谢。现在我怎么能对任何负数做同样的事情呢?@Kos我刚刚更新了我的答案。这是你想要的吗?