Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/283.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
Project Euler问题8 Python整数溢出_Python_Integer Overflow - Fatal编程技术网

Project Euler问题8 Python整数溢出

Project Euler问题8 Python整数溢出,python,integer-overflow,Python,Integer Overflow,该问题要求您“在1000位数字中找到具有最大乘积的十三个相邻数字”。对于以下数字: 7316717653133062491922511974426574742355349194934969835203127745063232573180169848018694788518438586156078911294954595017379583319528532088051112540699874715238305071569329096329522743043535766666666489504452

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

731671765313306249192251197442657474235534919493496983520312774506323257318016984801869478851843858615607891129495459501737958331952853208805111254069987471523830507156932909632952274304353576666666648950445231736403098711121723113622989342338033766152828282828280646445838383838383838353537373767676766152528282828282828282806445749390729072904925907713807190707070793535359195959545454545909090909090909090796086670172427121883998797908792274921901699720888093776657273330010533678812202354218097512545405947522435258490771167055601360483958644670632441572215539753697817977846174064955149290862569321978468622482839722413756570560574902614079729686524145351004748216637048440319989000889524345065854122758866688116427171479924442928230863465674813919123162824586178664583591245665294765456828489128831426076900422421902267105562632111093705544217506941658960408071984038509245544436298123098787992724428490918884580156660979133875420052406368991256071760605861164109405074100225691552000559357729725716362695618826708252483600823753042029450

我用Python编写的代码如下(我确信这不是最有效的):

导入numpy
a="7316717653133062491922511967442657474235534919493496983520312774506326239578318016984801869478851843858615607891129494954595017379583319528532088055111254069874715852386305071569329096329522744304355766896648950445244523161731856403098711121722383113622298934233803081353362766142828064444866452387493035890729629049156044077239071381051585930796086670172427121883998797908792274921901699720888093776657273330010533678812202354218097512545405947522435258490771167055601360483958644670632441572215539753697817977846174064955149290862569321978468622482839722413756570560574902614079729686524145351004748216637048440319989000889524345065854122758866688116427171479924442928230863465674813919123162824586178664583591245665294765456828489128831426076900422421902267105562632111110937054421750694165896040807198403850962455444362981230987879927244284909188845801561660979191338754992005240636899125607176060588611646710940507754100225698315520005593572972571636269561882670428252483600823257530420752963450"
res=0#我们想要退货的产品
reslst=[]#产品中元素的列表
i=0
而ires:
res=(numpy.prod(ilst))
reslst=ilst
i+=1
其他:
i+=1
打印(res,reslst)
但是,正如下面的文章中关于c++中的这个问题所述:

我得到了错误的答案2091059712[9,7,8,1,7,9,7,7,8,4,6,1,7]

但我在网上看到的每一个地方都有整数溢出、长整数和有符号整数,我对此并不熟悉,因为我对Python的经验有限

我肯定有一些关于Python的基本知识我一定不知道,所以提前谢谢你教我这个。

试试看

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

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

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

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]
导入numpy
a='7316717653133062491922511974426574742355349194934969835203127745063232578180169848018694788518438586156078911294954595017358331952853208805111254069874725715852383050715693290632952744355766666894895044523173640309871112722383113622893423380383508373627662766276614282828282826464464594949090796086670172427121883998797908792274921901699720888093776657273330010533678812202354218097512545405947522435258490771167055601360483958644670632441572215539753697817977846174064955149290862569321978468622482839722413756570560574902614079729686524145351004748216637048440319989000889524345065854122758866688116427171479924442928230863465674813919123162824586178664583591245665294765456828489128831426076900422421902267105562632111110937054421750694165896040807198403850962455444362981230987879927244284909188845801561660979191338754992005240636899125607176060588611646710940507754100225698315520005593572972571636269561882670428252483600823257530420752963450'
res=0#我们想要退货的产品
reslst=[]#产品中元素的列表
i=0
而ires:
res=(numpy.prod(ilst))
reslst=ilst
i+=1
其他:
i+=1
>>>打印(res,reslst)
23514624000 [5, 5, 7, 6, 6, 8, 9, 6, 6, 4, 8, 9, 5]

当您使用
numpy
时,所使用的数据类型如下:。基本上,您看到的是使用
C
时所看到的。您需要删除
numpy
,并在基本Python中手动计算产品。啊,我明白了,谢谢!这是否也意味着可能,就像在C中,peopl如果说使用long-long,你可以用np.longlong来解决这个问题?无论如何,我现在会手动计算产品。对我来说不是。我仍然得到
2091059712[9,7,8,1,7,9,7,7,8,4,6,1,7]
platform.python\u version()3.8.3rc1
numpy.\uuuuu version\uuuuuuu 1.18.4
是的,对我来说也是。对不起,山姆,我刚刚意识到我输入了一个字符串,但为了不想在这里再次输入,我忘了显示。我现在在这里编辑了我的代码,这给出了错误的答案。奇怪的是,我刚刚复制了你的代码并得到了正确的答案。我已经我正在使用的版本是:
python3.7.2
numpy1.16.3
非常完美,谢谢!这正是我在看到约瑟夫·伍德的帖子评论后考虑的。
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]