정보보안공부
python_day13_재귀함수2_곱셈함수,지수함수 본문
***곱셈함수 - 재귀함수로 표현 -> 반복되는 덧셈
예).
2 X 5 = 10
2 + 2 +2 +2 +2 = 10
5 + 5 = 10
multi(2,5)
1. 재귀함수
def recursion_multiple( x, y ) :
if y>0 :
return x + recursion_multiple( x, y-1)
else :
return 0
2. 꼬리재귀함수
def tail_recursion_multiple( x, y ) :
def loop( total, x, y ) :
if y>0 :
return loop( total +x, x, y-1)
else :
return total
return loop( 0, x, y )
3. 루프함수
def loop_multiple( x, y ) :
total = 0
while y>0 :
total += x
y -= 1
return total
*** 지수함수 - 재귀함수로 표현 -> 반복되는 곱셈
1. 재귀함수
def recursion_power( x, y) :
if y>0 :
return x*recursion_power(x,y-1)
else :
return 1
2. 꼬리재귀함수
def tail_recursion_power(x,y) :
def loop( save, x, y) :
if y>0 :
return loop(save*x, x, y-1)
else :
return save
return loop( 1, x, y )
3. 루프함수
def loop(x, y)
save = 1
while y>0 :
save *= x
y -= 1
return save
'Language > python' 카테고리의 다른 글
python_day15_선택정렬, 랜덤선택정렬 (0) | 2017.02.09 |
---|---|
python_day14_피보나치 수 (0) | 2017.02.09 |
python_day12_윤년구하기, 재귀함수 (0) | 2017.01.31 |
python_day11_파일 읽고 쓰기 (0) | 2017.01.31 |
python_day10_함수 (0) | 2017.01.25 |