1 개요[ | ]
- factorial()
- FACT()
- 팩토리얼을 구하는 함수
2 테스트케이스[ | ]
- factorial(0) = 1
- factorial(1) = 1
- factorial(2) = 2
- factorial(3) = 6
- factorial(4) = 24
- factorial(5) = 120
- factorial(6) = 720
- factorial(7) = 5040
- factorial(8) = 40320
- factorial(9) = 362880
- factorial(10) = 3628800
3 C[ | ]

C
CPU
0.1s
MEM
18M
0.1s
Copy
#include <stdio.h>
long long factorial(int num) {
if (num == 0) return 1;
return num * factorial(num - 1);
}
int main() {
printf("%lld\n", factorial(9)); // 362880
printf("%lld\n", factorial(12)); // 479001600
}
362880 479001600
4 Excel[ | ]

PHP
Copy
=FACT(4)
// 24
5 Java[ | ]
Java
CPU
1.2s
MEM
71M
0.8s
Copy
public class MyClass {
static int fact(int n) {
int res = 1;
for(int i=2; i<=n; i++) res *= i;
return res;
}
public static void main(String args[]){
System.out.println(fact(1)); // 1
System.out.println(fact(2)); // 2
System.out.println(fact(4)); // 24
System.out.println(fact(8)); // 40320
System.out.println(fact(9)); // 362880
System.out.println(fact(10)); // 3628800
System.out.println(fact(11)); // 39916800
System.out.println(fact(12)); // 479001600
System.out.println(fact(13)); // 1932053504 (wrong value, ≠ 6227020800)
System.out.println(fact(14)); // 1278945280 (wrong value, ≠ 87178291200)
}
}
1 2 24 40320 362880 3628800 39916800 479001600 1932053504 1278945280
6 JavaScript[ | ]

JavaScript
Copy
function factorial(n) {
var result = 1;
for(var i=2; i<=n; i++) result *= i;
return result;
}
7 Lua[ | ]

lua
Copy
function factorial(n)
if n == 0 then
return 1
end
return n * factorial(n - 1)
end
8 Perl[ | ]
Perl
Copy
sub factorial {
my ($x, $result) = (shift, 1);
$result *= $_ for (2 ... $x);
return $result;
}
9 PHP[ | ]

반복
PHP
Copy
function factorial($x) {
$result = 1;
for($i=2; $i<=$x; $i++) $result *= $i;
return $result;
}
echo factorial(4);
# 24
재귀
PHP
Copy
function factorial($n) {
if( $n <= 1 ) return 1;
return $n * factorial($n-1);
}
echo factorial(4);
# 24
10 Python[ | ]

Python
Copy
from math import factorial
print( factorial(4) )
Python
Copy
def factorial(x):
result = 1
for i in range(2,x+1):
result *= i
return result
11 R[ | ]

R
Copy
factorial(0) ## [1] 1
factorial(1) ## [1] 1
factorial(2) ## [1] 2
factorial(3) ## [1] 6
factorial(4) ## [1] 24
factorial(5) ## [1] 120
factorial(6) ## [1] 720
factorial(10) ## [1] 3628800
factorial(100) ## [1] 9.332622e+157
factorial(-1) ## [1] NaN
12 같이 보기[ | ]
편집자 Jmnote Jmnote bot
로그인하시면 댓글을 쓸 수 있습니다.
리눅스 Python 2.7 컴파일 설치 ― …리눅스 Python 2.7 컴파일 설치 ― …리눅스 Python 2.7 컴파일 설치 ― …리눅스 Python 2.7 컴파일 설치 ― …리눅스 Python 2.7 컴파일 설치 ― Jmnote리눅스 Python 2.7 컴파일 설치 ― ㅇㅇㅇ미운코딩새끼 ― 승호 도령미운코딩새끼 ― 불탄고등어미운코딩새끼 ― 김레이미운코딩새끼 ― 호박이미운코딩새끼 ― Junhg0211미운코딩새끼 ― 김왼손미운코딩새끼 ― 용딘이미운코딩새끼 ―Pinkcrimson
유기농냠냠파이썬 ― 호박유기농냠냠파이썬 ― 이에스유기농냠냠파이썬 ― 이승현파이썬 global ― Jmnote파이썬 global ― John Jeong파이썬 global ― Jmnote파이썬 global ― John Jeong파이썬 global ― John Jeong파이썬 global ― John Jeong파이썬 global ― Jmnote파이썬 global ― John Jeong