我遇到了另一个困难的Project Euler问题 Link to the problem
我的第一直觉是尝试一种简单的蛮力解决方案,这需要花费太多时间才能运行。
所以我想到了一个更好的解决方案,但我不知道如何编码。
我想:
我做了第1步,我的结果看起来像这样:
Multiples of 17: [[0, 1, 7], [0, 3, 4], [0, 5, 1], [0, 6, 8], [0, 8, 5], [1, 0, 2], [1, 3, 6], [1, 5, 3], [1, 7, 0], [1, 8, 7], [2, 0, 4], [2, 3, 8], [2, 8, 9], [3, 0, 6], [3, 4, 0], [3, 5, 7], [3, 7, 4], [3, 9, 1], [4, 0, 8], [4, 2, 5], [4, 5, 9], [4, 7, 6], [4, 9, 3], [5, 1, 0], [5, 2, 7], [5, 6, 1], [5, 7, 8], [6, 1, 2], [6, 2, 9], [6, 8, 0], [6, 9, 7], [7, 1, 4], [7, 3, 1], [7, 4, 8], [7, 6, 5], [7, 8, 2], [8, 1, 6], [8, 5, 0], [8, 6, 7], [9, 0, 1], [9, 1, 8], [9, 3, 5], [9, 5, 2], [9, 8, 6]] etc...
现在,这对我来说是棘手的部分。我尝试将它们与嵌套循环放在一起,但这确实很混乱。如果您有任何建议,请告诉我:)
答案 0 :(得分:1)
首先,蛮力解决方案几乎没有时间运行。
正如@MooingRawr建议的那样,如果你使用itertools.permutations
,只有 ~0.9 x 9! 0123456789 的排列不会以零。
from itertools import permutations
primes = [17, 13, 11, 7, 5, 3, 2]
total = 0
# Generate permutations of 10 distict digits -- 10 factorial
for i in permutations('0123456789'):
# Discard those that begin with zero -- one-tenth of 10!
if i[0] == '0':
continue
# Convert into a string and assume that it is valid
n = ''.join(list(i))
valid = True
# Check if the last three digits are divisible by 17, ...
# ... then shift left and check if those digits are divisible by 13, etc.
for j in xrange(0, len(primes)):
x = n[len(primes) - j:len(primes) - j + 3]
if int(x) % primes[j]:
valid = False
break
# Print and add
if valid:
print 'Valid number: %s' % n
total += int(n)
print 'Total: %d' % total
如果您运行此解决方案here,它会在几秒钟内运行,这对于PE来说应该没问题。
但是,您建议的方法确实更有效。请注意,您已经硬编码了七个循环,我只是使用factors
生成它们,其中factor[i]
是 d_i d_i + 1 d_i + 2 的因子。
您担心生成所有组合,但这很简单,使用递归,其中每次迭代检查最后两位数字并找到有效的下一位数。
factors = [1, 2, 3, 5, 7, 11, 13, 17]
valid_len = len(factors)
valid_sequences = []
total = 0
# Checks for a 3-digit number with 3 unique digits
def not_unique(digits):
return (digits[0] == digits[1]) or (digits[1] == digits[2]) or (digits[0] == digits[2])
# For each of the prime numbers, generate all valid triples that have unique digits
for i in xrange(0 ,len(factors)):
current_map = {}
for j in xrange(factors[i], 1000, factors[i]):
digits = str(j).zfill(3)
# Prune those numbers that have non-unique digits
if not_unique(digits):
continue
# current_map is of the form {'d1d2':[list of all possible valid d3s], ...}
if digits[:2] not in current_map:
current_map[digits[:2]] = [digits[2]]
else:
current_map[digits[:2]].append(digits[2])
valid_sequences.append(current_map)
# Checks each triple starting with the 3 most significant digits
# Get the last two digits, and find all the valid values for the next one digit
# Perform recursively
def get_matches_starting_with(sequence, index):
global total
if index == valid_len:
print 'Valid number: %s' % sequence
total += int(sequence)
else:
pair = sequence[-2:]
if pair in valid_sequences[index]:
for digit in valid_sequences[index][pair]:
if not digit in sequence:
get_matches_starting_with(sequence + digit, index + 1)
all_matches = []
for pair in valid_sequences[0]:
if pair[0] == '0':
continue
for digit in valid_sequences[0][pair]:
triple = pair + digit
all_matches.append(get_matches_starting_with(triple, 1))
print 'Total: %d' % total
您可能希望运行解决方案here,并可能在中间步骤打印值以查看正在进行的操作。
仍有很多机会修剪探索过的州的数量。你的方法将它从3265920降低到大约3000.