当前位置:首页 >> 编程语言 >> 【Python CheckiO 题解】Count Consecutive Summers,sony zx1

【Python CheckiO 题解】Count Consecutive Summers,sony zx1

0evadmin 编程语言 3
文件名:【Python CheckiO 题解】Count Consecutive Summers,sony zx1 【Python CheckiO 题解】Count Consecutive Summers

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) == 4count_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 andit'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)/2so n=(2p+a)*(a+1)/2so 2p+a=2n/(a+1)finally p=(2n-a-a^2)(2*(a+1))replacing a+1 with bp=(2n-b(b-1))/2band we know that p should b an integer, which means that 2n-b(b-1)%(2b)==0or 2n%(2b)==b(b-1)%(2b)Left part can be reduce to n%b*2Right 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 numbersThis 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"""
协助本站SEO优化一下,谢谢!
关键词不能为空
同类推荐
«    2025年12月    »
1234567
891011121314
15161718192021
22232425262728
293031
控制面板
您好,欢迎到访网站!
  查看权限
网站分类
搜索
最新留言
文章归档
网站收藏
友情链接