【问题标题】:How can I find the smallest possible number which is divisible by every number in a given list? [duplicate]如何找到可以被给定列表中的每个数字整除的最小可能数字? [复制]
【发布时间】:2017-09-20 03:06:33
【问题描述】:

我尝试用 python 2.7 解决这个问题。我刚开始编程,所以我希望你们中的一个了不起的人可以帮助我!

这是我目前拥有的:

my_list = [2,3,4,5,6,7,8,9,10,11,]


for a in range(0, 39916800):

    if (a % my_list == 0):
        print("a")

答案 =

a = 2*3*4*5*6*7*8*9*10*11

a = a-1

for a in range (0,a):
if a % 2 == 0:
    if a % 3 ==0:
        if a % 4 ==0:
            if a % 5 ==0:
                if a % 6 ==0:
                    if a % 7 ==0:
                        if a % 8 ==0:
                            if a % 9 ==0:
                                if a % 10 ==0:
                                    if a % 11 ==0:
                                        print a
                                        if a > 1:
                                            break

【问题讨论】:

  • 我看到了那个话题,但我并没有真正理解他们解决问题的方式。谁能告诉我我写的代码有什么问题以及我可以在哪里改进它。
  • 我找到了答案。 a = 2*3*4*5*6*7*8*9*10*11 a = a-1 对于范围内的 a (0,39916800):如果 a% 2 == 0:如果 a% 3 == 0: 如果% 4 ==0: 如果% 5 ==0: 如果% 6 ==0: 如果% 7 ==0: 如果% 8 ==0: 如果% 9 ==0:如果 a % 10 ==0:如果 a % 11 ==0:打印 a

标签: python python-2.7 python-3.x


【解决方案1】:

首先,我认为你的逻辑有点不对劲。我会遍历列表。

这段代码不能完全回答你的问题,但它似乎是一个很好的垫脚石

my_list = [2,3,4,5,6]

for a in my_list:
    for sub in range(len(my_list)): # nesting loop. pretty much for each num, it goes over each num in mylist again
        if a % my_list[sub] == 0:
            pass # nothing is required at this step, pass just allows the if to print or no nothing if the conditional is true
            if a == len(my_list):
                print(a)

【讨论】:

  • a = 2*3*4*5*6*7*8*9*10*11 a = a-1 for a in range (0,a): if a % 2 == 0:如果a % 3 ==0:如果a % 4 ==0:如果a % 5 ==0:如果a % 6 ==0:如果a % 7 ==0:如果a % 8 ==0: if a % 9 ==0: if a % 10 ==0: if a % 11 ==0: 打印 a if a > 1: break
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-19
  • 2022-10-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-11-10
相关资源
最近更新 更多