返回列表 发帖

[原创代码] 欧拉计划016-What is the sum of the digits of the number 2^1000?

欧拉计划016-What is the sum of the digits of the number 2^1000?
"""
python 欧拉计划016-What is the sum of the digits of the number 2^1000?
https://projecteuler.net/problem=16
2016年4月14日 13:37:24 codegay
"""
print(sum(map(int,list(str(2**1000)))))
"""
1366
[Finished in 0.1s]
"""COPY
去学去写去用才有进步。安装python3代码存为xx.py 双击运行或右键用IDLE打开按F5运行

#可以省掉list
sum(map(int,str(2**1000)))COPY
去学去写去用才有进步。安装python3代码存为xx.py 双击运行或右键用IDLE打开按F5运行

TOP

projecteuler.net 网站,别人贴的julia一行流
sum(digits(big(2)^1000))COPY
去学去写去用才有进步。安装python3代码存为xx.py 双击运行或右键用IDLE打开按F5运行

TOP

本帖最后由 happy886rr 于 2016-4-14 17:54 编辑

纯批处理,3.86秒,已经是极限速度了。
@echo off&setlocal enabledelayedexpansion
set "F[1]=00000002"
for /l %%i in (2 1 38) do (
set "F[%%i]=00000000"
)
for /l %%i in (2 1 1000) do (
set add=0
for /l %%j in (1 1 38) do (
set/a tmp=1!F[%%j]!+1!F[%%j]!+add
set/a add=!tmp:~0,1!-2
set F[%%j]=!tmp:~1!
)
)
for /l %%i in (1 1 38) do (
for /l %%j in (0 1 7) do (
set/a "sum+=!F[%%i]:~%%j,1!"
)
)
set/p=数字21000次方的各位数字之和为:!sum!COPY
----------------------------------------------------------------
python 0.05秒
# Python EulerPJ-016:What is the sum of the digits of the number 2^1000?
q=2**1000;r=0
while q>0:
r+=q%10;q//=10
print(r)COPY
二进制位操作不需要时间。

TOP

返回列表