问题描述
我正在用一种蛮力的方法来尝试找到谜题扩展的组合。
I'm doing a brute-force approach to trying to find the combination to an extension to a puzzle.
我正在尝试获取大量组合然后测试每个组合以查看它们是否符合特定条件。
我使用Python出色的itertools生成了组合,从本质上讲,这给了我一个可以检查每个迭代器的迭代器。
I am trying to get a large number of combinations and then test each combination to see if they fit certain criteria. I generate the combinations using Python's excellent itertools, essentially this gives me an iterator I can go over and test each one.
这很快就会返回并给我 91390 组合进行检查:
This returns quickly and gives me 91390 combinations to check:
itertools.combinations(range(1, 40), 4)
这需要几分钟,并给我 198792594 组合进行测试:
This takes a couple of minutes and give me 198792594 combinations to test:
itertools.combinations(range(1, 122), 5)
进入下一级别时,我需要得到以下答案:
When I get to the next level, I need the answer to this:
itertools.combinations(range(1, 365), 6)
当我进入6一组364的双向组合...需要很长时间。 年龄。我本质上是在要求很多组合吗?
When I get into 6-way combinations of a set of 364... it takes a very long time. AGES. Am I inherently asking for a great deal of combinations? How does it scale?
推荐答案
您可以像这样计算这些数字:
You calculate these numbers like this:
- 转到google.com
- 键入 40选择4
- 键入 121选择5
- 键入
- Go to google.com
- type in "40 choose 4"
- type in "121 choose 5"
- type in "364 choose 6"
请参见表示实际公式。
它像阶乘函数一样缩放。
It scales like the factorial function.
这篇关于itertools.combinations如何在Python中缩放?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!