Python 递归程序
我在编程方面相对较新,因为我受过数学家教育,并且没有 Python 经验。我想知道如何在Python中解决这个问题,这个问题是在我自己研究一个数学问题时出现的:
程序要求一个正整数m。如果 m 的形式为 2^n-1,则返回 T(m)=n*2^{n-1}。否则,它将 m 写入 2^n+x 的形式,其中 -1 < x < 2^n,并返回 T(m)=T(2^n-1)+x+1+T(x)。最后输出答案。
I'm relatively newcomer on programming as I'm educated a mathematician and have no experience on Python. I would like to know how to solve this problem in Python which appeared as I was studying one maths problem on my own:
Program asks a positive integer m. If m is of the form 2^n-1 it returns T(m)=n*2^{n-1}. Otherwise it writes m to the form 2^n+x, where -1 < x < 2^n, and returns T(m)=T(2^n-1)+x+1+T(x). Finally it outputs the answer.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为这是一个很好的问题,所以我尝试了一个解决方案。据我所知,这满足原始问题中的参数。
假设
calculate
函数的单元测试中包含的数字是问题的正确结果,则此解决方案应该是准确的。当然,我们非常欢迎您提供反馈。I thought this was a neat problem so I attempted a solution. As far as I can tell, this satisfies the parameters in the original question.
Assuming the numbers included in the
calculate
function's unit tests are the correct results for the problem, this solution should be accurate. Feedback is most welcome, of course.