Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/279.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python中的Euler#8项目 “” 在1000位数字中找出五个连续数字的最大乘积 ''' 导入时间 num='1\ 73167176531330624919225119674426574742355349194934\ 96983520312774506326239578318016984801869478851843\ 85861560789112949495459501737958331952853208805511\ 12540698747158523863050715693290963295227443043557\ 66896648950445244523161731856403098711121722383113\ 62229893423380308135336276614282806444486645238749\ 30358907296290491560440772390713810515859307960866\ 70172427121883998797908792274921901699720888093776\ 65727333001053367881220235421809751254540594752243\ 52584907711670556013604839586446706324415722155397\ 53697817977846174064955149290862569321978468622482\ 83972241375657056057490261407972968652414535100474\ 82166370484403199890008895243450658541227588666881\ 16427171479924442928230863465674813919123162824586\ 17866458359124566529476545682848912883142607690042\ 24219022671055626321111109370544217506941658960408\ 07198403850962455444362981230987879927244284909188\ 84580156166097919133875499200524063689912560717606\ 05886116467109405077541002256983155200055935729725\ 71636269561882670428252483600823257530420752963450' 最大值=0 i=1 而i最大: 最大=产品 i+=i+1 印刷品(产品) 开始=时间。时间() 已用=(time.time()-start) 打印(“此代码耗时:“+str(已用时间)+”秒”)_Python - Fatal编程技术网

Python中的Euler#8项目 “” 在1000位数字中找出五个连续数字的最大乘积 ''' 导入时间 num='1\ 73167176531330624919225119674426574742355349194934\ 96983520312774506326239578318016984801869478851843\ 85861560789112949495459501737958331952853208805511\ 12540698747158523863050715693290963295227443043557\ 66896648950445244523161731856403098711121722383113\ 62229893423380308135336276614282806444486645238749\ 30358907296290491560440772390713810515859307960866\ 70172427121883998797908792274921901699720888093776\ 65727333001053367881220235421809751254540594752243\ 52584907711670556013604839586446706324415722155397\ 53697817977846174064955149290862569321978468622482\ 83972241375657056057490261407972968652414535100474\ 82166370484403199890008895243450658541227588666881\ 16427171479924442928230863465674813919123162824586\ 17866458359124566529476545682848912883142607690042\ 24219022671055626321111109370544217506941658960408\ 07198403850962455444362981230987879927244284909188\ 84580156166097919133875499200524063689912560717606\ 05886116467109405077541002256983155200055935729725\ 71636269561882670428252483600823257530420752963450' 最大值=0 i=1 而i最大: 最大=产品 i+=i+1 印刷品(产品) 开始=时间。时间() 已用=(time.time()-start) 打印(“此代码耗时:“+str(已用时间)+”秒”)

Python中的Euler#8项目 “” 在1000位数字中找出五个连续数字的最大乘积 ''' 导入时间 num='1\ 73167176531330624919225119674426574742355349194934\ 96983520312774506326239578318016984801869478851843\ 85861560789112949495459501737958331952853208805511\ 12540698747158523863050715693290963295227443043557\ 66896648950445244523161731856403098711121722383113\ 62229893423380308135336276614282806444486645238749\ 30358907296290491560440772390713810515859307960866\ 70172427121883998797908792274921901699720888093776\ 65727333001053367881220235421809751254540594752243\ 52584907711670556013604839586446706324415722155397\ 53697817977846174064955149290862569321978468622482\ 83972241375657056057490261407972968652414535100474\ 82166370484403199890008895243450658541227588666881\ 16427171479924442928230863465674813919123162824586\ 17866458359124566529476545682848912883142607690042\ 24219022671055626321111109370544217506941658960408\ 07198403850962455444362981230987879927244284909188\ 84580156166097919133875499200524063689912560717606\ 05886116467109405077541002256983155200055935729725\ 71636269561882670428252483600823257530420752963450' 最大值=0 i=1 而i最大: 最大=产品 i+=i+1 印刷品(产品) 开始=时间。时间() 已用=(time.time()-start) 打印(“此代码耗时:“+str(已用时间)+”秒”),python,Python,这个代码给我的答案是7054,太低了,应该会计算很多产品,但只计算其中的9个。我的问题是:是什么导致我的代码偏离其预期目的,以及如何优化代码中计算“1”、“2”等的部分以计算产品?谢谢你的建议 问题在于: ''' Find the greatest product of five consecutive digits in the 1000-digit number ''' import time num = '\ 73167176531330624919225119674426574742

这个代码给我的答案是7054,太低了,应该会计算很多产品,但只计算其中的9个。我的问题是:是什么导致我的代码偏离其预期目的,以及如何优化代码中计算“1”、“2”等的部分以计算产品?谢谢你的建议

问题在于:

'''
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")
您每次都要加倍
i
,并向其添加
1
。因此,如果
i
为1,则将其设为3。如果是3,就等于7。如果是7,就等于15,依此类推。但这意味着你的索引缺少很多地方,不是吗

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

i += i+1
这只意味着将1添加到
i

或者你可以:

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

您的问题在于:

i = i+1
你浏览列表的速度太快了。您应该这样做:

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)

有几个问题

  • 您正在打印
    产品
    而不是
    最大的
    。确保打印正确的变量

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

  • 您将i变量的值递增错误。您希望每回合递增1,所以只需执行
    i+=1
    i=i+1
    。代码
    i+=i+1
    相当于
    i=i+1
    。我不认为您希望在while循环的每次迭代中都加倍
    i
    。)

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

  • 您没有正确地测量时间。您希望在所有代码执行之前分配
    开始时间
    时间,以便正确测量
    已用时间
    时间

  • 这是您的固定代码:

    def numprod(series):
        product = 1
        for c in series:
            product *= int(c)
        return product
    
    “”
    在1000位数字中找出五个连续数字的最大乘积
    '''
    导入时间
    开始=时间。时间()
    num='1\
    73167176531330624919225119674426574742355349194934\
    96983520312774506326239578318016984801869478851843\
    85861560789112949495459501737958331952853208805511\
    12540698747158523863050715693290963295227443043557\
    66896648950445244523161731856403098711121722383113\
    62229893423380308135336276614282806444486645238749\
    30358907296290491560440772390713810515859307960866\
    70172427121883998797908792274921901699720888093776\
    65727333001053367881220235421809751254540594752243\
    52584907711670556013604839586446706324415722155397\
    53697817977846174064955149290862569321978468622482\
    83972241375657056057490261407972968652414535100474\
    82166370484403199890008895243450658541227588666881\
    16427171479924442928230863465674813919123162824586\
    17866458359124566529476545682848912883142607690042\
    24219022671055626321111109370544217506941658960408\
    07198403850962455444362981230987879927244284909188\
    84580156166097919133875499200524063689912560717606\
    05886116467109405077541002256983155200055935729725\
    71636269561882670428252483600823257530420752963450'
    最大值=0
    i=0
    而i最大:
    最大=产品
    i=i+1
    打印(最大)
    已用=(time.time()-start)
    打印(“此代码耗时:“+str(已用时间)+”秒”)
    
    也可以使用函数式编程大大简化问题:

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

    再试一次!

    这不是优化的,但它适用于13位数字

    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
    
    导入时间
    开始时间=time.time()
    num=“73167176531330624919225119744265742355349194934\
    96983520312774506326239578318016984801869478851843\
    85861560789112949495459501737958331952853208805511\
    12540698747158523863050715693290963295227443043557\
    66896648950445244523161731856403098711121722383113\
    62229893423380308135336276614282806444486645238749\
    30358907296290491560440772390713810515859307960866\
    70172427
    
    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
    
    import time
    start_time = time.time()
    num = "73167176531330624919225119674426574742355349194934\
    96983520312774506326239578318016984801869478851843\
    85861560789112949495459501737958331952853208805511\
    12540698747158523863050715693290963295227443043557\
    66896648950445244523161731856403098711121722383113\
    62229893423380308135336276614282806444486645238749\
    30358907296290491560440772390713810515859307960866\
    70172427121883998797908792274921901699720888093776\
    65727333001053367881220235421809751254540594752243\
    52584907711670556013604839586446706324415722155397\
    53697817977846174064955149290862569321978468622482\
    83972241375657056057490261407972968652414535100474\
    82166370484403199890008895243450658541227588666881\
    16427171479924442928230863465674813919123162824586\
    17866458359124566529476545682848912883142607690042\
    24219022671055626321111109370544217506941658960408\
    07198403850962455444362981230987879927244284909188\
    84580156166097919133875499200524063689912560717606\
    05886116467109405077541002256983155200055935729725\
    71636269561882670428252483600823257530420752963450"
    i = 0
    j = 0
    k = 0
    while i < len(num) - 13:
        one = int(num[i])
        two = int(num[i + 1])
        three = int(num[i + 2])
        four = int(num[i + 3])
        five = int(num[i + 4])
        six = int(num[i + 5])
        seven = int(num[i + 6])
        eight = int(num[i + 7])
        nine = int(num[i + 8])
        ten = int(num[i + 9])
        eleven = int(num[i + 10])
        twoelve = int(num[i + 11])
        thirteen = int(num[i + 12])
        j = one * two * three * four * five * six * seven * eight * nine * ten * eleven * twoelve * thirteen
        i = i + 1
        if k < j:
            k = j
    print(k)
    print(time.time() - start_time," seconds")
    
    # 8 Largest product in a series
    
    from functools import reduce
    
    the_num = "7316717653133062491922511967442657474235534919493496983520312774506326239578318016984801869478851843858615607891129494954595017379583319528532088055111254069874715852386305071569329096329522744304355766896648950445244523161731856403098711121722383113622298934233803081353362766142828064444866452387493035890729629049156044077239071381051585930796086670172427121883998797908792274921901699720888093776657273330010533678812202354218097512545405947522435258490771167055601360483958644670632441572215539753697817977846174064955149290862569321978468622482839722413756570560574902614079729686524145351004748216637048440319989000889524345065854122758866688116427171479924442928230863465674813919123162824586178664583591245665294765456828489128831426076900422421902267105562632111110937054421750694165896040807198403850962455444362981230987879927244284909188845801561660979191338754992005240636899125607176060588611646710940507754100225698315520005593572972571636269561882670428252483600823257530420752963450"
    
    the_num_lst = []
    for n in the_num:
        the_num_lst.append(int(n))
    
    x = 0
    y = reduce((lambda a, b: a * b), the_num_lst[x:x + 13])
    while x < 1000 - 13:
        x += 1
        z = reduce((lambda a, b: a * b), the_num_lst[x:x + 13])
        if z > y:
            y = z
    print(y)
    
    s = '''7316717653133062491922511967442657474235534919493496983520312774506326239578318016984801869478851843858615607891129494954595017379583319528532088055111254069874715852386305071569329096329522744304355766896648950445244523161731856403098711121722383113622298934233803081353362766142828064444866452387493035890729629049156044077239071381051585930796086670172427121883998797908792274921901699720888093776657273330010533678812202354218097512545405947522435258490771167055601360483958644670632441572215539753697817977846174064955149290862569321978468622482839722413756570560574902614079729686524145351004748216637048440319989000889524345065854122758866688116427171479924442928230863465674813919123162824586178664583591245665294765456828489128831426076900422421902267105562632111110937054421750694165896040807198403850962455444362981230987879927244284909188845801561660979191338754992005240636899125607176060588611646710940507754100225698315520005593572972571636269561882670428252483600823257530420752963450'''
    
    
    t = list(s)
    #len(t) = 1000
    #t[::-1] = t[999]
    
    r = []
    q = 0
    while q <= 999:
        w = int(t[q])
        r.append(w)
        q += 1
    
    a = 0
    b = 1
    c = 2
    d = 3
    e = 4
    f = 5
    g = 6
    h = 7
    i = 8
    j = 9
    k = 10
    l = 11
    m = 12
    
    y = []
    
    while m<=999:
        n = r[a]*r[b]*r[c]*r[d]*r[e]*r[f]*r[g]*r[h]*r[i]*r[j]*r[k]*r[l]*r[m]
        y.append(n)
        a += 1
        b += 1
        c += 1
        d+= 1
        e+= 1
        f+= 1
        g+= 1
        h+= 1
        i+= 1
        j+= 1
        l+= 1
        k+= 1
        m+= 1
        if m >= 1000:
            break
    
    print(y)
    print(max(y))
    
    from functools import reduce
    def max_prod(x):
        return max([reduce(lambda x, y: int(x) * int(y), (x[i:i+5])) for i in range(len(x)-5)])