本帖最后由 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=数字2的1000次方的各位数字之和为:!sum!
复制代码 ----------------------------------------------------------------
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)
复制代码 二进制位操作不需要时间。 |