首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Project Euler问题8 Python整数溢出

Project Euler问题8 Python整数溢出
EN

Stack Overflow用户
提问于 2020-06-08 02:01:36
回答 2查看 131关注 0票数 0

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

7316717653133062491922511967442657474235534919493496983520312774506326239578318016984801869478851843858615607891129494954595017379583319528532088055111254069874715852386305071569329096329522744304355766896648950445244523161731856403098711121722383113622298934233803081353362766142828064444866452387493035890729629049156044077239071381051585930796086670172427121883998797908792274921901699720888093776657273330010533678812202354218097512545405947522435258490771167055601360483958644670632441572215539753697817977846174064955149290862569321978468622482839722413756570560574902614079729686524145351004748216637048440319989000889524345065854122758866688116427171479924442928230863465674813919123162824586178664583591245665294765456828489128831426076900422421902267105562632111110937054421750694165896040807198403850962455444362981230987879927244284909188845801561660979191338754992005240636899125607176060588611646710940507754100225698315520005593572972571636269561882670428252483600823257530420752963450

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

代码语言:javascript
复制
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++中这个问题的帖子所述:

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

但我在网上到处看到整数溢出、长整型和有符号整数,我并不熟悉,因为我只有有限的经验,还有Python。Googling似乎说整数溢出在python中不应该是问题。

我确信有一些关于Python的基础知识是我不能了解的,所以提前感谢您教给我这些。

EN

回答 2

Stack Overflow用户

发布于 2020-06-08 02:09:41

试一试

代码语言:javascript
复制
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

票数 1
EN

Stack Overflow用户

发布于 2020-06-08 02:11:17

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

代码语言:javascript
复制
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]
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62249562

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档