python - Python 中的欧拉计划 #8

标签 python

求 1000 位数字中连续五位数字的最大乘积:

import time

num = '\
73167176531330624919225119674426574742355349194934\
96983520312774506326239578318016984801869478851843\
85861560789112949495459501737958331952853208805511\
12540698747158523863050715693290963295227443043557\
66896648950445244523161731856403098711121722383113\
62229893423380308135336276614282806444486645238749\
30358907296290491560440772390713810515859307960866\
70172427121883998797908792274921901699720888093776\
65727333001053367881220235421809751254540594752243\
52584907711670556013604839586446706324415722155397\
53697817977846174064955149290862569321978468622482\
83972241375657056057490261407972968652414535100474\
82166370484403199890008895243450658541227588666881\
16427171479924442928230863465674813919123162824586\
17866458359124566529476545682848912883142607690042\
24219022671055626321111109370544217506941658960408\
07198403850962455444362981230987879927244284909188\
84580156166097919133875499200524063689912560717606\
05886116467109405077541002256983155200055935729725\
71636269561882670428252483600823257530420752963450'

biggest = 0
i = 1
while i < len(num):
    one = int(num[i]) 
    two = int(num[i+1])  
    thr = int(num[i+2]) 
    fou = int(num[i+3])
    fiv = int(num[i+4])
    product = one*two*thr*fou*fiv
    if product > biggest:
        biggest = product
    i += i+1 
 print(product)

 start = time.time()
 elapsed = (time.time() - start)
 print("This code took: " + str(elapsed) + " seconds")

这段代码给我的答案是 7054,太低了,一路上应该计算很多产品,但只计算了其中的 9 个。我的问题是:是什么导致我的代码偏离其预期目的,以及如何优化代码中计算“一”、“二”等的部分以计算乘积?

最佳答案

有几个问题。

  1. 您打印的是 product 而不是 biggest 。确保打印正确的变量!

  2. 您正在遍历整个字符串的长度,而实际上您应该在 [0..len(num) - 4) 范围内进行迭代,这样您在进行乘积计算时就不会遇到 IndexError。

  3. 您错误地增加了 i 变量。你想每回合增加 1,所以只需执行 i += 1i = i + 1 。代码 i += i + 1 等同于 i = i + i + 1 。我认为您不想在 while 循环的每次迭代中都将 i 加倍。 :)

  4. 序列在 Python 中是从 0 开始索引的。这意味着当您遍历一组索引时,第一个元素始终位于 seq[0] 并且元素继续直到 seq[n-1] 。因此,您的 i 变量应该从 0 而不是 1 开始!

  5. 您没有正确计算时间。您希望在执行所有代码之前分配 start 时间,以便您可以正确测量 elapsed 时间。

这是你的固定代码:

'''
Find the greatest product of five consecutive digits in the 1000-digit number
'''

import time
start = time.time()

num = '\
73167176531330624919225119674426574742355349194934\
96983520312774506326239578318016984801869478851843\
85861560789112949495459501737958331952853208805511\
12540698747158523863050715693290963295227443043557\
66896648950445244523161731856403098711121722383113\
62229893423380308135336276614282806444486645238749\
30358907296290491560440772390713810515859307960866\
70172427121883998797908792274921901699720888093776\
65727333001053367881220235421809751254540594752243\
52584907711670556013604839586446706324415722155397\
53697817977846174064955149290862569321978468622482\
83972241375657056057490261407972968652414535100474\
82166370484403199890008895243450658541227588666881\
16427171479924442928230863465674813919123162824586\
17866458359124566529476545682848912883142607690042\
24219022671055626321111109370544217506941658960408\
07198403850962455444362981230987879927244284909188\
84580156166097919133875499200524063689912560717606\
05886116467109405077541002256983155200055935729725\
71636269561882670428252483600823257530420752963450'

biggest = 0
i = 0
while i < len(num) - 4:
    one = int(num[i]) 
    two = int(num[i+1])  
    thr = int(num[i+2]) 
    fou = int(num[i+3])
    fiv = int(num[i+4])
    product = one*two*thr*fou*fiv
    if product > biggest:
        biggest = product
    i = i + 1 
print(biggest)

elapsed = (time.time() - start)
print("This code took: " + str(elapsed) + " seconds")

关于python - Python 中的欧拉计划 #8,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19285079/

相关文章:

python - 使用 Pandas 通过键标识符转置多列

python - Django 中全局数据存储在哪里

python - 与 Django 的 'regroup' 模板标签等效的惯用 Python 是什么?

python - 在 Bokeh 中使用 x 轴上的月份

python - ArangoDB:尝试使用 gharial 接口(interface)创建顶点时出现服务器错误

python - 查找第一次出现的偶数

python - 在ubuntu上将django sqlite数据库迁移到mysql

python - 如何在 matplotlib 中用文本注释热图

python - 文件存在于目录中,但正在获取 “No such file or directory: ' I_90-0-109_(90).txt'”

Python/Django - 按天和周按总和构建列表