2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

再帰

Posted at

同じく書籍より


def fact(x):
    if x == 1:
        return 1
    else:
        return x * fact(x-1) #再帰で階乗を計算している

fact(3) > 6 #321 = 6となる 3!階乗計算

fact(3)呼び出しで
return x * fact(x-1) # 3 x 2
すぐにfact(2)呼ばれて
return x * fact(x-1) # (3*2) x 1
みたいな階乗計算(スタックのように積み重なっているイメージかな)

2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?