Python中的Project Euler#8

2024-06-02 07:53:58 发布

您现在位置:Python中文网/ 问答频道 /正文

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

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个。我的问题是:是什么导致我的代码偏离了它的预期目的,同时,我如何优化代码中计算“一”、“二”等的部分来计算产品?谢谢你的建议!


Tags: the代码timeproductonestartnumint
3条回答

你的问题是:

i += i+1

你看单子太快了。你应该这样做:

i += 1

我会这样写代码:

import operator

# Return the product of all the digits in `series` converted to integers
def numprod(series):
    # Convert the series of digits into a list of integers
    digits = [int(c) for c in series]

    # This applies the multiplication operator to all the digits,
    # starting with 1
    return reduce(operator.__mul__, digits, 1)

# Produce every string of length 5
# This uses a generator but could just as easily use a list comprehension:
# numiter = [num[i : i + SERIES_SIZE] for i in range(len(num) - SERIES_SIZE + 1)]
SERIES_SIZE = 5
numiter = (num[i : i + SERIES_SIZE] for i in range(len(num) - SERIES_SIZE + 1))

# Calculate all the products
allnumprods = [numprod(series) for series in numiter]

# Find the maximum of all the products
print max(allnumprods)

计算产品的一种更简单的方法是:

def numprod(series):
    product = 1
    for c in series:
        product *= int(c)
    return product

有几个问题。

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

  2. 您在遍历整个字符串的长度时,实际上应该在[0..len(num) - 4)范围内迭代,这样在进行产品计算时就不会得到索引器错误。

  3. 你的i变量递增错误。你想每转增加1次,所以只要做i += 1i = i + 1。代码i += i + 1相当于i = i + i + 1。我认为您不想在while循环的每个迭代上都加倍。:)

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

  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")

问题在于:

i += i+1

你每次都要把i翻一番,然后加上1。所以如果i是1,那么就设为3。如果是3,你就得7。如果是7,你就得15,以此类推。但这意味着你的索引漏掉了很多地方,不是吗!

这会导致你跳过数字中的许多位置。您要使用:

i += 1

这只意味着在i中添加1。

或者你可以:

i = i+1

这意味着,将i设置为i+1

相关问题 更多 >