小编Ara*_*wal的帖子

在Python中计算给定数字列表的LCM

我编写了一个代码来查找数字列表的LCM(最低公倍数)但我的代码中似乎有错误.代码如下:

def final_lcm(thelist):
   previous_thelist = thelist
   prime_thelist = list(set(thelist) - set(returns_new_thelist(previous_thelist))
   factors = 1
   for i in prime_thelist:
       factors = factors*i
   new_thelist = returns_new_thelist(previous_thelist)
   for i in range(1, 10000000000):
       s_empty = []
       for j in new_thelist:
           if i % j  == 0:
               s_empty.append(True)
       if len(new_thelist) == len(s_empty):
           initial_lcm = i
           break
   final_lcm = factor*initial_lcm
   return final_lcm



def returns_new_thelist(ll):
    if 3 in ll:
        ll.remove(3)
    for i in ll:
        if checks_if_prime(i) == True:
            ll.remove(i)
    return ll    

def checks_if_prime(n):
    if n == 2: …
Run Code Online (Sandbox Code Playgroud)

python

8
推荐指数
5
解决办法
2万
查看次数

标签 统计

python ×1