Python 练习实例26
题目:利用递归方法求5!。
程序分析:递归公式:fn=fn_1*4!
程序源代码:
#!/usr/bin/python # -*- coding: UTF-8 -*- def fact(j): sum = 0 if j == 0: sum = 1 else: sum = j * fact(j - 1) return sum for i in range(5): print '%d! = %d' % (i,fact(i))
以上实例输出结果为:
0! = 1 1! = 1 2! = 2 3! = 6 4! = 24