Latest web development tutorials

Pythonは演習26と、

Pythonの100リー Pythonの100リー

タイトル:再帰的なメソッド要求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

Pythonの100リー Pythonの100リー