2013-10-10 42 views
3
''' 
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個。我的問題是:什麼導致我的代碼偏離其預期目的,以及爲了計算產品如何優化計算「一個」,「兩個」等的代碼部分?感謝您的任何建議!Python中的項目Euler#8

+1

首先,Python是0-索引語言。在你的代碼中你從1開始,所以你跳過第一個元素。 – Anycorn

回答

0

問題就在這裏:

i += i+1 

i每次加倍和增加1它。所以,如果i是1,那麼你做它3.如果它是3,你做它7.如果它是7,你做它15,依此類推。但這意味着你的索引缺少很多地方,不是!

這會導致您跳過編號中的許多位置。你想使用:

i += 1 

這只是意味着加1到i

或者你可以這樣做:

i = i+1 

這意味着,設置i等於i+1

2

你的問題是在這裏:

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 
+2

我覺得初學者很難搞清楚這個示例:) – Anycorn

+2

提問者看起來對Python來說很新穎,我猜你的版本可能太多了,無法處理。 – Joseph

+2

公平的評論。我甚至會讚揚你的批評。也許有人做歐拉項目的問題會從這個答案中得到一些東西。 :-) – hughdbrown

9

有幾個問題。

  1. 您在打印productbiggest。確保打印正確的變量!

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

這是一個相當全面的答案。它涵蓋了代碼中的所有錯誤,並僅使用列出的修補程序重寫代碼。 – hughdbrown

0
import string 
numbers=list() 
fo=open("C:/python34/libs/maths2.txt","r+") 
for eachline in fo: 
    for char in eachline: 
     if char.isdigit(): 
      A=char 
      numbers.append(int(A)) 

print(numbers) 
list1=list() 
for i in range(0,len(numbers)-4): 
    x=numbers[i]*numbers[i+1]*numbers[i+2]*numbers[i+3]*numbers[i+4] 
    list1.append([x]) 
print(list1) 
print(max(list1)) 
+0

請您詳細說明您的答案,並增加關於您提供的解決方案的更多描述。 – abarisone

0

你也可以使用函數編程大大簡化問題:

def foo(): 
    max_product = 0 
    num = "string of input number" 
    for e,n in enumerate(num): 
     product = reduce((lambda x,y: x*y),map(int,(num[e:e+13]))) 
     if product > max_product: 
      max_product = product 
    return max_product 

試試看!