CheckiO 是面向初学者和高级程序员的编码游戏,使用 Python 和 JavaScript 解决棘手的挑战和有趣的任务,从而提高你的编码技能,本博客主要记录自己用 Python 在闯关时的做题思路和实现代码,同时也学习学习其他大神写的代码。

CheckiO 官网:https://checkio.org/

我的 CheckiO 主页:https://py.checkio.org/user/TRHX/

CheckiO 题解系列专栏:https://itrhx.blog.csdn.net/category_9536424.html

CheckiO 所有题解源代码:https://github.com/TRHX/Python-CheckiO-Exercise


题目描述

【Count Consecutive Summers】:一个正整数可以用几个连续的正整数之和来表示,例如,正整数42,可以有四种方法来表示:(1)3 + 4 + 5 + 6 + 7 + 8 + 9、(2)9 + 10 + 11 + 12、(3)13 + 14 +15、(4)42,其中第四种方法表示该正整数仅由其本身组成,你的任务是计算有多少种表示方法。

【链接】:https://py.checkio.org/mission/count-consecutive-summers/

【输入】:整数

【输出】:整数

【前提】:输入始终是一个正整数

【范例】

count_consecutive_summers(42) == 4
count_consecutive_summers(99) == 6

代码实现

def count_consecutive_summers(num):n = 0for i in range(1,num+1):m = iwhile m < num:i,m = i+1,m+i+1if m == num:n += 1return nif __name__ == '__main__':print("Example:")print(count_consecutive_summers(42))# These "asserts" are used for self-checking and not for an auto-testingassert count_consecutive_summers(42) == 4assert count_consecutive_summers(99) == 6assert count_consecutive_summers(1) == 1print("Coding complete? Click 'Check' to earn cool rewards!")

大神解答

大神解答 NO.1

count_consecutive_summers = lambda n: sum(not n%k for k in range(1, n+1, 2))

大神解答 NO.2

def count_consecutive_summers(num):# s + (s + 1) + (s + 2) + ... + (s + n)# = (n + 1) * s + n * (n + 1) / 2 = num# s = (num - n * (n + 1) / 2) / (n + 1)# we need denominator of s is divisible by n + 1count = 0for n in range(0, num): # we move nd = num - n * (n + 1) // 2 # calculate denominatorif d <= 0: breakif d % (n + 1) == 0: # s is integer?count += 1return count

大神解答 NO.3

def count_consecutive_summers(num):ways = 0for i in range(1, num + 1):n = 1 - 2 * i + ((2 * i - 1) ** 2 + 8 * num) ** (1/2)if n % 1 == 0:ways += 1return ways

大神解答 NO.4

count_consecutive_summers=lambda n:sum([n%b*2==b-b%2*b for b in range(1,int((2*n)**.5)+1)])
"""
Is this really working?
Yes it is, it's one of the shortest code to solve the problem and
it's doing it very efficiently as well2nd version
-----------
How does it work:
sum from p to p+a when p and a are integers is (2p+a)*(a+1)/2
so n=(2p+a)*(a+1)/2
so 2p+a=2n/(a+1)
finally p=(2n-a-a^2)(2*(a+1))
replacing a+1 with b
p=(2n-b(b-1))/2b
and we know that p should b an integer, which means that 2n-b(b-1)%(2b)==0
or 2n%(2b)==b(b-1)%(2b)
Left part can be reduce to n%b*2
Right part can be reduce to (b-1)%2*b which can be written b-b%2*b for 2 characters lessValues for a are from a=0 (sum of a single value) to a=0.5*(-1+sqrt(1+8*n)) when we have the lonest sum of numbers
This value does not make a nice 'golf' code, but 0.5*(-1+sqrt(1+8*n)) is just slightly smaller than sqrt(2n)
for n=1 000 000, this means that the program will evaluate a up to 1414 instead of 1413, acceptable trade off for a shorter code :)
Values for b are therefore from 1 to sqrt(2n)+12nd version uses:
- b in place of a+1 for a lot of save characters
- modulo instead of int(x)==x to define if x is integer, greatly save chars but it's much faster (20-30%)It was a great exerice so found a way to solve it for both code size and speed
"""

【Python CheckiO 题解】Count Consecutive Summers相关推荐

  1. Python CheckiO 题解系列 丨 博客目录索引

    CheckiO 是面向初学者和高级程序员的编码游戏,使用 Python 和 JavaScript 解决棘手的挑战和有趣的任务,从而提高你的编码技能,本题解系列主要记录自己在用 Python 闯关时的做 ...

  2. 【Python CheckiO 题解】Army Battles

    CheckiO 是面向初学者和高级程序员的编码游戏,使用 Python 和 JavaScript 解决棘手的挑战和有趣的任务,从而提高你的编码技能,本博客主要记录自己用 Python 在闯关时的做题思 ...

  3. 【Python CheckiO 题解】Multicolored Lamp

    CheckiO 是面向初学者和高级程序员的编码游戏,使用 Python 和 JavaScript 解决棘手的挑战和有趣的任务,从而提高你的编码技能,本博客主要记录自己用 Python 在闯关时的做题思 ...

  4. 【Python CheckiO 题解】Feed Pigeons

    CheckiO 是面向初学者和高级程序员的编码游戏,使用 Python 和 JavaScript 解决棘手的挑战和有趣的任务,从而提高你的编码技能,本博客主要记录自己用 Python 在闯关时的做题思 ...

  5. 【Python CheckiO 题解】Striped Words

    CheckiO 是面向初学者和高级程序员的编码游戏,使用 Python 和 JavaScript 解决棘手的挑战和有趣的任务,从而提高你的编码技能,本博客主要记录自己用 Python 在闯关时的做题思 ...

  6. 【Python CheckiO 题解】Popular Words

    CheckiO 是面向初学者和高级程序员的编码游戏,使用 Python 和 JavaScript 解决棘手的挑战和有趣的任务,从而提高你的编码技能,本博客主要记录自己用 Python 在闯关时的做题思 ...

  7. 【Python CheckiO 题解】Second Index

    CheckiO 是面向初学者和高级程序员的编码游戏,使用 Python 和 JavaScript 解决棘手的挑战和有趣的任务,从而提高你的编码技能,本博客主要记录自己用 Python 在闯关时的做题思 ...

  8. 【Python CheckiO 题解】Sort Array by Element Frequency

    CheckiO 是面向初学者和高级程序员的编码游戏,使用 Python 和 JavaScript 解决棘手的挑战和有趣的任务,从而提高你的编码技能,本博客主要记录自己用 Python 在闯关时的做题思 ...

  9. 【Python CheckiO 题解】Non-unique Elements

    CheckiO 是面向初学者和高级程序员的编码游戏,使用 Python 和 JavaScript 解决棘手的挑战和有趣的任务,从而提高你的编码技能,本博客主要记录自己用 Python 在闯关时的做题思 ...

最新文章

  1. linux 临时设置英文输入,Linux 设置 Rime 默认英文状态
  2. 对接接口文档_接口自动化测试框架设计思路
  3. xcode 允许SVN管理项目文件
  4. str字符串 encoding( ) 方法
  5. 设置更改root密码 ,连接mysql,mysql常用命令
  6. 正三角形的外接圆面积
  7. C# 不支持关键字: “.;database”。
  8. mysql explain字段含义_史上最全的explain常见结果含义分析,值得收藏
  9. 产品开发版本的延续性
  10. 发那科机器人圆弧指令怎么用_发那科机器人走弧线的指令是什么
  11. 一篇文章彻底搞清楚Windows系统批处理脚本.bat文件的注释
  12. 使用vscode如何建立vue模板
  13. html添加启动项,windows boot manager启动项是什么
  14. blender导入png图片不透明 | png贴图在blender中有黑底
  15. Proguard的Keep使用方法
  16. SQL/MYSQL在CMD命令操作符中创建数据库 、表单并插入数据查看
  17. sql 纵向求和_SQL求和
  18. skipping incompatible xxxx.a when searching for -lxxx问题的解决
  19. java 一对多、多对多关系示例
  20. Iviews视频搜索引擎

热门文章

  1. toolStrip 按钮图片大小的修改
  2. CSS中class优先级问题
  3. CruiseControl.NET ----- mail 配置
  4. learning to rank评价指标
  5. 计算机服务哪些不能关闭,Win7系统下哪些系统服务不能关闭
  6. project 模板_施工进度横道图不会做?18份计划模板收藏好,输入参数迅速成图...
  7. maven pc配置要求_《使命召唤:黑色行动5》公开测试PC配置要求:推荐GTX970+i7
  8. oracle字段加约束,Oracle数据库的字段约束创建和维护示例
  9. 如何查询oracle的共享内存,[20190104]ipcs查看共享内存段.txt
  10. 602B. Approximating a Constant Range