Project Euler问题8 Python整数溢出

问题描述 投票:0回答:2

该问题要求您“查找1000位数字中具有最大乘积的13位相邻数字。”对于以下数字:

73167176531330624919225119674426574742355349194934969835203127745063262395783180169848018694788518438586156078911294949545950173795833195285320880551112540698747158523863050715693290963295227443043557668966489504452445231617318564030987111217223831136222989342338030813533627661428280644448664523874930358907296290491560440772390713810515859307960866701724271218839987979087922749219016997208880937766572733300105336788122023542180975125454059475224352584907711670556013604839586446706324415722155397536978179778461740649551492908625693219784686224828397224137565705605749026140797296865241453510047482166370484403199890008895243450658541227588666881164271714799244429282308634656748139191231628245861786645835912456652947654568284891288314260769004224219022671055626321111109370544217506941658960408071984038509624554443629812309878799272442849091888458015616609791913387549920052406368991256071760605886116467109405077541002256983155200055935729725716362695618826704282524836008232575304207529634 50

我在Python中的代码如下(我确定这不是最有效的::

import numpy

a= "7316717653133062491922511967442657474235534919493496983520312774506326239578318016984801869478851843858615607891129494954595017379583319528532088055111254069874715852386305071569329096329522744304355766896648950445244523161731856403098711121722383113622298934233803081353362766142828064444866452387493035890729629049156044077239071381051585930796086670172427121883998797908792274921901699720888093776657273330010533678812202354218097512545405947522435258490771167055601360483958644670632441572215539753697817977846174064955149290862569321978468622482839722413756570560574902614079729686524145351004748216637048440319989000889524345065854122758866688116427171479924442928230863465674813919123162824586178664583591245665294765456828489128831426076900422421902267105562632111110937054421750694165896040807198403850962455444362981230987879927244284909188845801561660979191338754992005240636899125607176060588611646710940507754100225698315520005593572972571636269561882670428252483600823257530420752963450"

res=0       #the product we want to be returned
reslst=[]    #the list of the elements in the product
i = 0
while i<988:
    ilst=[int(a[j]) for j in range(i,i+13)]
    if numpy.prod(ilst)>res:
        res = (numpy.prod(ilst))
        reslst = ilst
        i +=1
    else:
        i +=1
print(res,reslst)

但是在以下有关c ++中此问题的文章中:

Project Euler #8, I don't understand where I'm going wrong我得到了错误的答案2091059712 [9,7,8,1,7,9,7,7,8,4,4,1,1,7]

但是我在网上看到的所有地方都建议整数溢出,长整型和有符号整数,因为我经验有限,我不熟悉它们,并且使用Python。谷歌搜索似乎说整数溢出在python中应该不是问题。

我肯定我对Python不太了解,所以在此先感谢您教我。

python integer-overflow
2个回答
1
投票

尝试

ilst=np.array([int(a[j]) for j in range(i,i+13)],dtype=np.longlong)

然后得到23514624000 [5 5 7 6 6 8 9 6 6 4 8 9 5]


0
投票

键是将数字表示为字符串。您的代码对我来说是正确的:

import numpy
a = '7316717653133062491922511967442657474235534919493496983520312774506326239578318016984801869478851843858615607891129494954595017379583319528532088055111254069874715852386305071569329096329522744304355766896648950445244523161731856403098711121722383113622298934233803081353362766142828064444866452387493035890729629049156044077239071381051585930796086670172427121883998797908792274921901699720888093776657273330010533678812202354218097512545405947522435258490771167055601360483958644670632441572215539753697817977846174064955149290862569321978468622482839722413756570560574902614079729686524145351004748216637048440319989000889524345065854122758866688116427171479924442928230863465674813919123162824586178664583591245665294765456828489128831426076900422421902267105562632111110937054421750694165896040807198403850962455444362981230987879927244284909188845801561660979191338754992005240636899125607176060588611646710940507754100225698315520005593572972571636269561882670428252483600823257530420752963450'
res=0       #the product we want to be returned
reslst=[]    #the list of the elements in the product
i = 0
while i<988:
    ilst=[int(a[j]) for j in range(i,i+13)]
    if numpy.prod(ilst)>res:
        res = (numpy.prod(ilst))
        reslst = ilst
        i +=1
    else:
        i +=1
>>> print(res,reslst)

23514624000 [5, 5, 7, 6, 6, 8, 9, 6, 6, 4, 8, 9, 5]
© www.soinside.com 2019 - 2024. All rights reserved.