Skip to content

Latest commit

ย 

History

History
41 lines (31 loc) ยท 850 Bytes

2019-05-26-AT-factorial.md

File metadata and controls

41 lines (31 loc) ยท 850 Bytes

2019๋…„ 5์›” 8์ผ

ํŒฉํ† ๋ฆฌ์–ผ {docsify-ignore-all}

๊ฐœ์ธ์ ์œผ๋กœ ๊ณต๋ถ€ํ•œ ๊ณผ์ •์„ ์ •๋ฆฌํ•ด ๋†“์€ ๊ธ€์ž…๋‹ˆ๋‹ค. ๋‚ด์šฉ ์ƒ ์˜ค๋ฅ˜๊ฐ€ ์žˆ์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ž˜๋ชป๋œ ๋ถ€๋ถ„์ด ์žˆ์„ ๊ฒฝ์šฐ ๋Œ“๊ธ€์— ๋‚จ๊ฒจ์ฃผ์‹œ๋ฉด ๊ฐ์‚ฌํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค :)

์ •์˜

์ถœ์ฒ˜: https://ko.wikipedia.org/wiki/%EA%B3%84%EC%8A%B9

  • 1 ๋ถ€ํ„ฐ n ๊นŒ์ง€ ๋ชจ๋“  ์ž์—ฐ์ˆ˜์˜ ๊ณฑ์„ ๋งํ•œ๋‹ค.

๋‹ค์–‘ํ•œ ํ’€์ด๋ฒ•


Method #1

def factorial(n):
    if n == 0:
        return 1
    fact = 1
    for i in range(1,n+1):
        fact = fact * i
    return fact

Method #2

def factorial_bottom_up(n):
    if n == 0:
        return 1
    bottom_up = [None]*(n+1)
    bottom_up[0] = 1
    bottom_up[1] = 1
    bottom_up[2] = 2
    for i in range(2,n+1):
        bottom_up[i] = i* bottom_up[i-1]
    return bottom_up[n]