python - Project Euler #13 理解(Python)

标签 python

问题 13:http://projecteuler.net/problem=13

计算出以下一百个 50 位数字之和的前十位数字。 那么,问题总和是5000位,答案是结果的前10位吗?

bignumber = list of the 5000 digits
sum(bignumber) = abcdefghijklmnopqrst...    
answer = abcdefghj

好吧,当我这样做时 sum(bignumber) = 22660(甚至不是 10 位数字)...

我是不是看错了问题?

def foo():
    with open ("bignumber", "r") as myfile:
        data=myfile.read().replace('\n', '')
    data = map(long, data)
    datasum = sum(data)
    return (datasum)

最佳答案

你误读了这个问题。

他们给你 100 个数字,你需要求和,每个数字有 50 位长(也就是 X*10^50 的量级)。那里有 50 位数字部分,因此您不能只使用传统的 int/long 数据类型(正如 JLLAgrange 指出的那样,这部分对于 python 来说应该不是问题,因为整数没有最大值)。

关于python - Project Euler #13 理解(Python),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20124928/

相关文章:

python - 将列拆分为多列

python - 不同对象中的复选框和单选按钮同时在所有对象中使用react(不希望的)

Python:从 NxM 正态分布中抽取的 NxM 样本数组

python - 模拟 bash 的 [ctrl]-l,**清爽**

python - Flask-sqlalchemy group_by 具有最大 id

python - 将概率与 scikit-learn 中的标签联系起来

python - 如何构建这个嵌套的 for 循环?

python - 如何将 pandas value_counts 转换为 python 列表

python - 在 python 中动态生成的 XLSXWriter 图表 - 不引用

python - PyMySQL 一次查询中的不同更新?