Project Euler第8题代码求助:为何计算结果不正确?
Hey there! Let's dig into why your code is returning the wrong result for Project Euler Problem 8, and fix it up step by step.
First, let's recap what your code is aiming to do: take that massive number string, clean it up, split it into 13-digit chunks, calculate the product of each chunk's digits, then find the maximum product. Makes perfect sense for the problem—you just missed one small but critical detail.
The Key Bug: Forgetting to Reset the Product Variable
Look at the loop where you calculate the product of each chunk:
for m in k: for z in m: l *= int(z) f.append(l)
You initialize l = 1 once at the top of your code, but you never reset it to 1 before processing each new 13-digit chunk. That means every subsequent product is multiplied by the previous chunk's product—so you're not calculating the product of each individual chunk, you're building a running total product of all chunks combined up to that point. No wonder your result is way off!
How to Fix It
You need to reset l to 1 inside the loop over each chunk m. This ensures every chunk starts with a fresh product calculation. Here's the corrected section:
for m in k: l = 1 # Reset product for each new chunk for z in m: l *= int(z) f.append(l)
Bonus: Small Optimization
Your initial loop range range(0,988) works (since the cleaned string is 1000 characters long, 1000-13+1=988), but you can make it more dynamic by using len(n) - 12 instead of hardcoding 988. This way, if the input length ever changes (not that it will here), your code still works:
for i in range(len(n) - 12): k.append(n[i:i+13])
Full Corrected Code
Putting it all together, here's the code that will give you the correct answer:
n = """ 73167176531330624919225119674426574742355349194934 96983520312774506326239578318016984801869478851843 85861560789112949495459501737958331952853208805511 12540698747158523863050715693290963295227443043557 66896648950445244523161731856403098711121722383113 62229893423380308135336276614282806444486645238749 30358907296290491560440772390713810515859307960866 70172427121883998797908792274921901699720888093776 65727333001053367881220235421809751254540594752243 52584907711670556013604839586446706324415722155397 53697817977846174064955149290862569321978468622482 83972241375657056057490261407972968652414535100474 82166370484403199890008895243450658541227588666881 16427171479924442928230863465674813919123162824586 17866458359124566529476545682848912883142607690042 24219022671055626321111109370544217506941658960408 07198403850962455444362981230987879927244284909188 84580156166097919133875499200524063689912560717606 05886116467109405077541002256983155200055935729725 71636269561882670428252483600823257530420752963450 """ n = n.strip().split('\n') n = ''.join(n) k = [] f = [] for i in range(len(n) - 12): k.append(n[i:i+13]) for m in k: l = 1 for z in m: l *= int(z) f.append(l) print(max(f))
This should now compute the correct maximum product of 13 adjacent digits in the large number.
内容的提问来源于stack exchange,提问作者vedatbaday

