找回密码
 注册
搜索
[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
查看: 13331|回复: 3

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

[复制链接]
发表于 2016-4-14 13:43:04 | 显示全部楼层 |阅读模式
欧拉计划016-What is the sum of the digits of the number 2^1000?
  1. """
  2. python 欧拉计划016-What is the sum of the digits of the number 2^1000?
  3. https://projecteuler.net/problem=16
  4. 2016年4月14日 13:37:24 codegay
  5. """

  6. print(sum(map(int,list(str(2**1000)))))

  7. """
  8. 1366
  9. [Finished in 0.1s]
  10. """
复制代码
 楼主| 发表于 2016-4-14 13:55:42 | 显示全部楼层

  1. #可以省掉list
  2. sum(map(int,str(2**1000)))
复制代码
 楼主| 发表于 2016-4-14 14:01:37 | 显示全部楼层
projecteuler.net 网站,别人贴的julia一行流
  1. sum(digits(big(2)^1000))
复制代码
发表于 2016-4-14 17:07:32 | 显示全部楼层
本帖最后由 happy886rr 于 2016-4-14 17:54 编辑

纯批处理,3.86秒,已经是极限速度了。

  1. @echo off&setlocal enabledelayedexpansion
  2. set "F[1]=00000002"
  3. for /l %%i in (2 1 38) do (
  4.         set "F[%%i]=00000000"
  5. )
  6. for /l %%i in (2 1 1000) do (
  7.         set add=0
  8.         for /l %%j in (1 1 38) do (
  9.                 set/a tmp=1!F[%%j]!+1!F[%%j]!+add
  10.                 set/a add=!tmp:~0,1!-2
  11.                 set F[%%j]=!tmp:~1!
  12.         )
  13. )
  14. for /l %%i in (1 1 38) do (
  15.         for /l %%j in (0 1 7) do (
  16.                 set/a "sum+=!F[%%i]:~%%j,1!"
  17.         )
  18. )
  19. set/p=数字2的1000次方的各位数字之和为:!sum!
复制代码
----------------------------------------------------------------
python 0.05秒

  1. # Python EulerPJ-016:What is the sum of the digits of the number 2^1000?
  2. q=2**1000;r=0
  3. while q>0:
  4.         r+=q%10;q//=10
  5. print(r)
复制代码
二进制位操作不需要时间。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|手机版|小黑屋|批处理之家 ( 渝ICP备10000708号 )

GMT+8, 2026-3-17 00:48 , Processed in 0.019866 second(s), 8 queries , File On.

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

快速回复 返回顶部 返回列表