【发布时间】:2021-06-13 23:47:00
【问题描述】:
我正在编写一个程序来计算 100 的阶乘数字和。我们不允许使用循环结构或有程序状态;我们只应该使用递归。这意味着我将首先计算100!然后将答案中的每个数字加在一起。例如:10! = 3628800。3+6+2+8+8+0+0 = 27。
该程序使用 2 个递归循环设置。第一个循环是一个简单的递归结构,计算 n 的阶乘。使用 n = 100 可以正常运行。
接下来,为了使用递归添加数字,编写了一个由 3 个组件组成的函数。
- n % 10:这将隔离 n 的最后一位数字
- math.floor( n / 10 ) :一旦数字被隔离,我们需要将其从数字中删除。为此,我们将 n 除以 10 并向下舍入到最接近的整数。
- return (n % 10) + Summation(math.floor(n / 10)):这将在递归调用中添加每个孤立的数字
此程序在输入 22 时完美运行。但是,当尝试计算 23! 的数字总和时,math.floor(n % 10) 不能正确除法。
这里的大问题是,是什么让这个计算在如此高的值下不正确?这是否与 Python 的精度水平有关?谢谢!
代码:
'''
--------------------------------------------------------
Problem 20:
n! means n × (n − 1) × ... × 3 × 2 × 1
For example, 10! = 10 × 9 × ... × 3 × 2 × 1 = 3628800,
and the sum of the digits in the number 10! is 3 + 6 + 2 + 8 + 8 + 0 + 0 = 27.
Find the sum of the digits in the number 100!
----------------------------------------------------------
'''
import math
def Divide(n):
return float(n/10)
'''
How to sum the digits:
First, we access the last digit by using Mod 10, which gives us the remainder
Second, in order to dispose of that digit (as we have already retrieved it),
we divide the number by 10 and round down to the nearest integer. This is done by
using math.floor(x)
Lastly, we add the retrieved digit to the recursive call of Summation that passes
through the rounded-down, divided number
'''
def Summation(n):
if n <= 0:
return n
else:
print("---------------------")
print("Number: ", n)
print("Mod: ", n%10)
print("Divide: ", str(n/10))
print("New Num: ", math.floor(Divide(n)))
return (n % 10) + Summation(math.floor(Divide(n)))
def Factorial(n):
if n == 1:
return n
else:
return n * Factorial(n-1)
def Main(n):
return Summation(Factorial(n))
'''
To run the program: call Main(100). Then, on the first printed segment, compare
Number to New Num. The only difference between these numbers is that New Num should
have the last digit removed since we divided by 10 and got rid of the decimal. However,
as you can see, the number changes drastically after this simple computation. If you
scroll more towards the bottom, you can see this method work correctly.
'''
【问题讨论】:
-
不要使用
float。只需使用为此量身定制的整数,因为 Python 整数可以任意大。您根本不需要导入math。使用整数除法(//运算符)和 mod(%运算符)。