Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/339.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 计数数字总和与乘积相同的范围内的数字_Python_Algorithm_Python 3.x_For Loop_Range - Fatal编程技术网

Python 计数数字总和与乘积相同的范围内的数字

Python 计数数字总和与乘积相同的范围内的数字,python,algorithm,python-3.x,for-loop,range,Python,Algorithm,Python 3.x,For Loop,Range,我有一个大范围的数字(从10^5到10^6),我需要计算所有数字的总和与乘积相同 例如,111126 1+1+1+1+2+6=12 1*1*1*1*2*6=12 我试过使用下面的代码,它工作得很好,但速度很慢,因为数字范围很大 result = 0 for num in range(x, y): num_str = str(num) summation = 0 product = 1 for j in range(len(num_str)):

我有一个大范围的数字(从10^5到10^6),我需要计算所有数字的总和与乘积相同

例如,111126

  • 1+1+1+1+2+6=12
  • 1*1*1*1*2*6=12
我试过使用下面的代码,它工作得很好,但速度很慢,因为数字范围很大

result = 0

for num in range(x, y):
    num_str = str(num)
    summation = 0
    product = 1
    for j in range(len(num_str)):
        summation += int(num_str[j])
        product *= int(num_str[j])
        if j == len(num_str) - 1:
            if summation == product:
                result += 1
print(result)

有没有办法不使用循环来计算这些数字?如果没有,如何使它运行得更快?

通常加速循环的方法是将它们更改为numpy数组,并执行数组操作而不是循环。因此,如果您将数字放入2D numpy数组,然后执行数组操作,例如a==B,并在新数组中计算trues,通常会快得多

也许还有比暴力更好的方法。我不清楚您的范围,因为您给出的示例超出了您给出的范围,但您可以看到最大和为5x9=45。您可以立即删除任何素数和大于10的素数


例如,您可以遍历5个数字a、b、c、d、e的所有不同可能乘积,因此1加速循环的常用方法是将它们更改为numpy数组,并执行数组操作而不是循环。因此,如果您将数字放入2D numpy数组,然后执行数组操作,例如a==B,并在新数组中计算trues,通常会快得多

也许还有比暴力更好的方法。我不清楚您的范围,因为您给出的示例超出了您给出的范围,但您可以看到最大和为5x9=45。您可以立即删除任何素数和大于10的素数


例如,您可以遍历5个数字a、b、c、d、e的所有不同可能乘积,因此1加速循环的常用方法是将它们更改为numpy数组,并执行数组操作而不是循环。因此,如果您将数字放入2D numpy数组,然后执行数组操作,例如a==B,并在新数组中计算trues,通常会快得多

也许还有比暴力更好的方法。我不清楚您的范围,因为您给出的示例超出了您给出的范围,但您可以看到最大和为5x9=45。您可以立即删除任何素数和大于10的素数


例如,您可以遍历5个数字a、b、c、d、e的所有不同可能乘积,因此1加速循环的常用方法是将它们更改为numpy数组,并执行数组操作而不是循环。因此,如果您将数字放入2D numpy数组,然后执行数组操作,例如a==B,并在新数组中计算trues,通常会快得多

也许还有比暴力更好的方法。我不清楚您的范围,因为您给出的示例超出了您给出的范围,但您可以看到最大和为5x9=45。您可以立即删除任何素数和大于10的素数


例如,您可以遍历5个数字a、b、c、d、e的所有不同可能的乘积,因此1您根本不需要强制执行此操作,因为您可以极大地限制搜索

  • 任何带有0的数字都将导致乘积为0,而总和将大于0。不要理会这些数字
  • 数字顺序无关紧要。
    1+2
    的总和与
    2+1
    的总和相同,这同样适用于它们的乘积
更好的方法是关注具有相等或递增数字的数字,如果这些数字的总和与乘积的值相同,则采用这些数字的所有唯一排列

要生成候选编号,只有1287个置换,置换范围为1到9之间的5位数字:

>>> import itertools
>>> len(list(itertools.combinations_with_replacement(range(1, 10), 5)))
1287
这是一个更小的搜索空间:

from itertools import combinations_with_replacement, permutations
from operator import mul
from functools import reduce

results = set()
for digits in combinations_with_replacement(range(1, 10), 5):
    if sum(digits) == reduce(mul, digits):
        # add unique permutations of the digits as a new integer
        results.update(int(''.join(map(str, p))) for p in permutations(digits))

for result in sorted(results):
    print(result)
这将在很短的时间内产生40个结果:

>>> from itertools import combinations_with_replacement, permutations
>>> from operator import mul
>>> from functools import reduce
>>> results = set()
>>> for digits in combinations_with_replacement(range(1, 10), 5):
...     if sum(digits) == reduce(mul, digits):
...         results.update(int(''.join(map(str, p))) for p in permutations(digits))
... 
>>> len(results)
40
>>> for result in sorted(results):
...     print(result)
... 
11125
11133
11152
11215
11222
11251
11313
11331
11512
11521
12115
12122
12151
12212
12221
12511
13113
13131
13311
15112
15121
15211
21115
21122
21151
21212
21221
21511
22112
22121
22211
25111
31113
31131
31311
33111
51112
51121
51211
52111

搜索范围可能会进一步缩小;例如,通过更多的搜索,您可以缩小对至少有2个
1
数字的数字的搜索范围,但上面的搜索速度已经相当快了。

您根本不需要强制执行此操作,因为您可以大大限制搜索

  • 任何带有0的数字都将导致乘积为0,而总和将大于0。不要理会这些数字
  • 数字顺序无关紧要。
    1+2
    的总和与
    2+1
    的总和相同,这同样适用于它们的乘积
更好的方法是关注具有相等或递增数字的数字,如果这些数字的总和与乘积的值相同,则采用这些数字的所有唯一排列

要生成候选编号,只有1287个置换,置换范围为1到9之间的5位数字:

>>> import itertools
>>> len(list(itertools.combinations_with_replacement(range(1, 10), 5)))
1287
这是一个更小的搜索空间:

from itertools import combinations_with_replacement, permutations
from operator import mul
from functools import reduce

results = set()
for digits in combinations_with_replacement(range(1, 10), 5):
    if sum(digits) == reduce(mul, digits):
        # add unique permutations of the digits as a new integer
        results.update(int(''.join(map(str, p))) for p in permutations(digits))

for result in sorted(results):
    print(result)
这将在很短的时间内产生40个结果:

>>> from itertools import combinations_with_replacement, permutations
>>> from operator import mul
>>> from functools import reduce
>>> results = set()
>>> for digits in combinations_with_replacement(range(1, 10), 5):
...     if sum(digits) == reduce(mul, digits):
...         results.update(int(''.join(map(str, p))) for p in permutations(digits))
... 
>>> len(results)
40
>>> for result in sorted(results):
...     print(result)
... 
11125
11133
11152
11215
11222
11251
11313
11331
11512
11521
12115
12122
12151
12212
12221
12511
13113
13131
13311
15112
15121
15211
21115
21122
21151
21212
21221
21511
22112
22121
22211
25111
31113
31131
31311
33111
51112
51121
51211
52111

搜索范围可能会进一步缩小;例如,通过更多的搜索,您可以缩小对至少有2个
1
数字的数字的搜索范围,但上面的搜索速度已经相当快了。

您根本不需要强制执行此操作,因为您可以大大限制搜索

  • 任何带有0的数字都将导致乘积为0,而总和将大于0。不要理会这些数字
  • 数字顺序无关紧要。
    1+2
    的总和与
    2+1
    的总和相同,这同样适用于它们的乘积
更好的方法是关注具有相等或递增数字的数字,如果这些数字的总和与乘积的值相同,则采用这些数字的所有唯一排列

要生成候选编号,只有1287个置换,置换范围为1到9之间的5位数字:

>>> import itertools
>>> len(list(itertools.combinations_with_replacement(range(1, 10), 5)))
1287
这是一个更小的搜索空间:

from itertools import combinations_with_replacement, permutations
from operator import mul
from functools import reduce

results = set()
for digits in combinations_with_replacement(range(1, 10), 5):
    if sum(digits) == reduce(mul, digits):
        # add unique permutations of the digits as a new integer
        results.update(int(''.join(map(str, p))) for p in permutations(digits))

for result in sorted(results):
    print(result)
这位职业选手