Notice
Recent Posts
Recent Comments
Link
«   2025/02   »
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28
Archives
Today
Total
관리 메뉴

RUBY

팩토리얼(파이썬 출력) 본문

프로그래밍 언어/Python

팩토리얼(파이썬 출력)

ruby-jieun 2023. 1. 14. 13:26

 

팩토리얼

 


 

 

 

팩토리얼

그 수보다 작거나 같은 모든 양의 정수의 곱

 

 

 

 

팩토리얼 결과값 출력

 

1. 반복문 이용하기

inputN = int(input('n 입력: '))

result = 1
for n in range(1, inputN+1):
    result *= n
print('{} 팩토리얼: {}'.format(inputN, result))
inputN = int(input('n 입력: '))

result = 1
n = 1
while n <= inputN:
    result *= n
    n += 1

print('{} 팩토리얼: {}'.format(inputN, result))

 

2. 재귀 함수 이용하기

inputN = int(input('n 입력: '))

def factorialFun(n):
    if n == 1: return 1

    return n * factorialFun(n - 1)

print('{} 팩토리얼: {}'.format(inputN, factorialFun(inputN)))

 

3. 팩토리얼 함수 사용하기

inputN = int(input('n 입력: '))

import math
math.factorial(inputN)

print('{} 팩토리얼: {}'.format(inputN, math.factorial(inputN)))

 

 

 

'프로그래밍 언어 > Python' 카테고리의 다른 글

순열(파이썬 출력)  (0) 2023.01.14
군수열(파이썬 출력)  (0) 2023.01.14
피보나치 수열(파이썬 출력)  (0) 2023.01.14
계차 수열(파이썬 출력)  (0) 2023.01.14
시그마(파이썬 출력)  (0) 2023.01.14
Comments