print("# n๋ฒ์งธ ํผ๋ณด๋์น ์๋ฅผ ๋ฆฌํด")# 0 1 1 2 3 5 8 13 21 ...deffib(n): if n ==1or n ==2:# base casereturn1else:# recursive casereturnfib(n-1)+fib(n-2)# ํ ์คํธ: fib(1)๋ถํฐ fib(10)๊น์ง ์ถ๋ ฅfor i inrange(1, 11):print(fib(i))
ํฉ
print("# 1~10๊น์ง ํฉ๊ตฌํ๊ธฐ")deftriangle_number(n):if n ==1:# base casereturn1return n +triangle_number(n -1)# recursive casefor i inrange(1, 11):print(triangle_number(i))
print("# n์ ๊ฐ ์๋ฆฟ์์ ํฉ์ ๋ฆฌํด")# n = 123 โ 1+2+3=6defsum_digits(n):if n <10:# base casereturn nelse:# recursive case divide = n //10 remain = n %10returnsum_digits(divide)+ remain# ํ ์คํธprint(sum_digits(22541))print(sum_digits(92130))print(sum_digits(12634))print(sum_digits(704))print(sum_digits(3755))