簡體   English   中英

在一個數字序列中找到最大的 n 位乘積

[英]Finding the largest n-digit product in a sequence of numbers

目的是在長度> n的數字范圍內找到n位數字,乘積最高。 例如,在下面的 1000 位數字中,找到它們的乘積最大的 13 位數字序列。

import numpy as np
n = 13
# for visual readibility the number is truncated in this example
x = '''73167176531330624919225117...2483600823257530420752963450'''

#make a list of all possible thirteen adjacent digits with no 0    
y = [x[i:i+n] for i in range(len(x)) if '0' not in x[i:i+n] and i+n < len(x)]

#convert to int and multiply     
y3d = [np.array(list(y[i]), dtype=int) for i in range(len(y))]     
multiplied = [np.prod(y3d[i]) for i in range(len(y3d))]

print(max(multiplied))

數字 =

它在 1 到 12 位數字上工作,但我在 13 位上很難

歡迎來到 SO @Gelar Anugerah。

我不確定您的代碼是否適用於 4 位數字。

問題是未定義變量“multy”,您應該將其替換為

    print(max(multiplied))

然后你應該得到max的索引並打印正確的序列:

    j = multiplied.index(max(multiplied))
    print(y3d[j])

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM