logo

숫자의 계승을 위한 프로그램

숫자의 계승이란 무엇입니까?

  • 음수가 아닌 정수의 계승은 n보다 작거나 같은 모든 양의 정수의 곱셈입니다. 예를 들어 6의 계승은 6*5*4*3*2*1, 즉 720입니다.
  • 계승은 숫자와 !로 표시됩니다. 마지막에 표시하세요. 가능한 총 결과를 계산하기 위해 순열 및 조합에 널리 사용됩니다. 프랑스 수학자 Christian Kramp가 처음으로 느낌표를 사용했습니다.

추천 실습 팩토리얼 시도해 보세요!

재귀 함수를 사용하여 계승 프로그램을 만들어 보겠습니다. 값이 0이 아닐 때까지 재귀 함수는 자체적으로 호출됩니다. 팩토리얼은 다음 재귀 공식을 사용하여 계산할 수 있습니다.

N! =n * (n – 1)!
N! n = 0 또는 n = 1인 경우 = 1



구현은 다음과 같습니다.

C++




// C++ program to find> // factorial of given number> #include> using> namespace> std;> > // Function to find factorial> // of given number> unsigned>int> factorial(unsigned>int> n)> > >if> (n == 0> > // Driver code> int> main()> {> >int> num = 5;> >cout <<>'Factorial of '> ><< num <<>' is '> << factorial(num) << endl;> >return> 0;> }> // This code is contributed by Shivi_Aggarwal>

>

본어게인 쉘
>




// C program to find factorial of given number> #include> > // function to find factorial of given number> unsigned>int> factorial(unsigned>int> n)> {> >if> (n == 0)> >return> 1;> >return> n * factorial(n - 1);> }> > int> main()> {> >int> num = 5;> >printf>(>'Factorial of %d is %d'>, num, factorial(num));> >return> 0;> }>

>

>

자바




// Java program to find factorial of given number> class> Test {> >// method to find factorial of given number> >static> int> factorial(>int> n)> >{> >if> (n ==>0>)> >return> 1>;> > >return> n * factorial(n ->1>);> >}> > >// Driver method> >public> static> void> main(String[] args)> >{> >int> num =>5>;> >System.out.println(>'Factorial of '> + num> >+>' is '> + factorial(>5>));> >}> }>

>

>

파이썬3




# Python 3 program to find> # factorial of given number> > # Function to find factorial of given number> def> factorial(n):> > >if> n>=>=> 0>:> >return> 1> > >return> n>*> factorial(n>->1>)> > # Driver Code> num>=> 5>;> print>(>'Factorial of'>, num,>'is'>,> factorial(num))> > # This code is contributed by Smitha Dinesh Semwal>

>

>

씨#




// C# program to find factorial> // of given number> using> System;> > class> Test {> >// method to find factorial> >// of given number> >static> int> factorial(>int> n)> >{> >if> (n == 0)> >return> 1;> > >return> n * factorial(n - 1);> >}> > >// Driver method> >public> static> void> Main()> >{> >int> num = 5;> >Console.WriteLine(>'Factorial of '> >+ num +>' is '> + factorial(5));> >}> }> > // This code is contributed by vt_m>

>

>

PHP




// PHP program to find factorial // of given number // function to find factorial // of given number function factorial($n) { if ($n == 0) return 1; return $n * factorial($n - 1); } // Driver Code $num = 5; echo 'Factorial of ', $num, ' is ', factorial($num); // This code is contributed by m_kit ?>>

>

>

자바스크립트




> // Javascript to find factorial> // of given number> > // function to find factorial> // of given number> function> factorial(n) {> >if> (n == 0)>return> 1;> >return> n * factorial(n - 1);> }> > // Driver Code> let num = 5;> document.write(>'Factorial of '> + num +>' is '> + factorial(num));> > // This code is contributed by Saurabh Jaiswal> > >

>

>

산출

Factorial of 5 is 120>

시간 복잡도: 에)
보조 공간: 에)

숫자의 계승을 찾는 반복 솔루션:

재귀는 많은 수의 경우 비용이 많이 들 수 있으므로 계승은 반복적으로 계산할 수도 있습니다. 여기서는 for 루프와 while 루프를 모두 사용하는 반복적 접근 방식을 보여주었습니다.

접근법 1: For 루프 사용

문제를 해결하려면 다음 단계를 따르세요.

  • for 루프를 사용하여 숫자의 계승을 찾는 프로그램을 작성하겠습니다.
  • 프로그램에서는 값이 1인 정수 변수가 사용됩니다.
  • 반복할 때마다 값은 사용자가 입력한 값과 같아질 때까지 1씩 증가합니다.
  • 사용자가 입력한 숫자의 계승값이 사실 변수의 최종 값이 됩니다.

다음은 위의 접근 방식을 구현한 것입니다.

C++




// C++ program for factorial of a number> #include> using> namespace> std;> > // function to find factorial of given number> unsigned>int> factorial(unsigned>int> n)> {> >int> res = 1, i;> >for> (i = 2; i <= n; i++)> >res *= i;> >return> res;> }> > // Driver code> int> main()> {> >int> num = 5;> >cout <<>'Factorial of '> ><< num <<>' is '> ><< factorial(num) << endl;> >return> 0;> }> > // This code is contributed by Shivi_Aggarwal>

>

>




#include> > // function to find factorial of given number> unsigned>int> factorial(unsigned>int> n)> {> >int> res = 1, i;> >for> (i = 2; i <= n; i++)> >res *= i;> >return> res;> }> > int> main()> {> >int> num = 5;> >printf>(> >'Factorial of %d is %d'>, num, factorial(num));> >return> 0;> }>

>

np 표준
>

자바




// Java program to find factorial of given number> class> Test {> > >// Method to find factorial of the given number> >static> int> factorial(>int> n)> >{> >int> res =>1>, i;> >for> (i =>2>; i <= n; i++)> >res *= i;> >return> res;> >}> > >// Driver method> >public> static> void> main(String[] args)> >{> >int> num =>5>;> >System.out.println(> >'Factorial of '> + num> >+>' is '> + factorial(>5>));> >}> }>

>

>

파이썬3




# Python 3 program to find> # factorial of given number> > # Function to find factorial of given number> def> factorial(n):> > >res>=> 1> > >for> i>in> range>(>2>, n>+>1>):> >res>*>=> i> >return> res> > # Driver Code> num>=> 5>;> print>(>'Factorial of'>, num,>'is'>,> factorial(num))> > # This code is contributed by Smitha Dinesh Semwal>

>

>

씨#




// C# program to find> // factorial of given number> using> System;> > class> Test {> >// Method to find factorial> >// of given number> >static> int> factorial(>int> n)> >{> >int> res = 1, i;> > >for> (i = 2; i <= n; i++)> >res *= i;> >return> res;> >}> > >// Driver method> >public> static> void> Main()> >{> >int> num = 5;> >Console.WriteLine(> >'Factorial of '> + num> >+>' is '> + factorial(5));> >}> }> > // This code is contributed by vt_m>

>

>

PHP




// function to find factorial // of given number function factorial( $n) { $res = 1; $i; for ($i = 2; $i <= $n; $i++) $res *= $i; return $res; } // Driver Code $num = 5; echo 'Factorial of ', $num, ' is ', factorial($num); // This code is contributed // by anuj_67. ?>>

>

>

자바스크립트




> // JavaScript program to find factorial of given number> > >// Method to find factorial of the given number> >function> factorial(n)> >{> >var> res = 1, i;> >for> (i = 2; i <= n; i++)> >res *= i;> >return> res;> >}> > >// Driver method> > >var> num = 5;> >document.write(>'Factorial of '> + num +>' is '> + factorial(5));> > > // This code is contributed by shivanisinghss2110.> > >

>

>

산출

Factorial of 5 is 120>

시간 복잡도: 에)
보조 공간: 오(1)

접근법 2: 이 예제에서는 while 루프를 사용하여 알고리즘을 구현하고 계승 프로그램을 찾습니다.




// C program for factorial of a number> #include> > // function to find factorial of given number> unsigned>int> factorial(unsigned>int> n)> {> >if>(n == 0)> >return> 1;> >int> i = n, fact = 1;> >while> (n / i != n) {> >fact = fact * i;> >i--;> >}> >return> fact;> }> > int> main()> {> >int> num = 5;> >printf>(>'Factorial of %d is %d'>, num, factorial(num));> >return> 0;> }>

>

>

C++




// C++ program for factorial of a number> #include> using> namespace> std;> > // function to find factorial of given> // number using while loop> unsigned>int> factorial(unsigned>int> n)> {> >if>(n == 0)> >return> 1;> >int> i = n, fact = 1;> >while> (n / i != n) {> >fact = fact * i;> >i--;> >}> >return> fact;> }> > // Driver code> int> main()> {> >int> num = 5;> >cout <<>'Factorial of '> ><< num <<>' is '> ><< factorial(num) << endl;> >return> 0;> }> // This code is contributed by Shivi_Aggarwal>

>

타이프스크립트 화살표 기능
>

자바




// Java program to find factorial of given number> > class> Test {> > >// Method to find factorial of the given number> >static> int> factorial(>int> n)> >{> >if>(n ==>0>)> >return> 1>;> >int> i = n, fact =>1>;> >while> (n / i != n) {> >fact = fact * i;> >i--;> >}> >return> fact;> >}> > >// Driver method> >public> static> void> main(String[] args)> >{> >int> num =>5>;> >System.out.println(> >'Factorial of '> + num> >+>' is '> + factorial(>5>));> >}> }>

>

>

파이썬3




# Python 3 program to find> # factorial of given number> > # Function to find factorial of given number> def> factorial(n):> >if>(n>=>=> 0>):> >return> 1> >i>=> n> >fact>=> 1> > >while>(n>/> i !>=> n):> >fact>=> fact>*> i> >i>->=> 1> > >return> fact> > # Driver Code> num>=> 5>;> print>(>'Factorial of'>, num,>'is'>,> factorial(num))> > # This code is contributed by Smitha Dinesh Semwal>

>

>

씨#




// C# program to find> // factorial of given number> using> System;> > class> Test {> >// Method to find factorial> >// of given number> >static> int> factorial(>int> n)> >{> >if>(n == 0)> >return> 1;> >int> i = n, fact = 1;> >while> (n / i != n) {> >fact = fact * i;> >i--;> >}> >return> fact;> >}> > >// Driver method> >public> static> void> Main()> >{> >int> num = 5;> >Console.WriteLine(> >'Factorial of '> + num> >+>' is '> + factorial(5));> >}> }>

>

>

굵은 글씨용 CSS

자바스크립트




> >// JavaScript Program to implement> >// the above approach> >// function to find factorial of given> >// number using while loop> >function> factorial(n) {> >if> (n == 0)> >return> 1;> >let i = n, fact = 1;> >while> (Math.floor(n / i) != n) {> >fact = fact * i;> >i--;> >}> >return> fact;> >}> > >// Driver code> >let num = 5;> >document.write(>'Factorial of '> >+ num +>' is '> >+ factorial(num) +>' '>);> > // This code is contributed by Potta Lokesh> > >>

>

>

산출

Factorial of 5 is 120>

시간 복잡도: 에)
보조 공간: 오(1)

접근법 3:삼항 연산자 if…else 문의 약어로 생각할 수 있습니다. 조건과 그에 따라 실행될 명령문이 제공됩니다. 다음은 삼항 연산자를 사용하여 계승을 위한 프로그램입니다.

C++




// C++ program to find factorial of given number> #include> using> namespace> std;> > int> factorial(>int> n)> > >// single line to find factorial> >return> (n == 1> > // Driver Code> int> main()> {> >int> num = 5;> >cout <<>'Factorial of '> << num <<>' is '><< factorial(num);> >return> 0;> }> > // This code is contributed by shivanisinghss2110>

>

>




// C++ program to find factorial of given number> #include> > int> factorial(>int> n)> n == 0) ? 1 : n * factorial(n - 1);> > > // Driver Code> int> main()> {> >int> num = 5;> >printf>(>'Factorial of %d is %d'>, num, factorial(num));> >return> 0;> }> > // This code is contributed by Rithika palaniswamy.>

>

>

자바




// Java program to find factorial> // of given number> class> Factorial {> > >int> factorial(>int> n)> > n ==>0>) ?>1> : n * factorial(n ->1>);> >> > >// Driver Code> >public> static> void> main(String args[])> >{> >Factorial obj =>new> Factorial();> >int> num =>5>;> >System.out.println(> >'Factorial of '> + num> >+>' is '> + obj.factorial(num));> >}> }> > // This code is contributed by Anshika Goyal.>

>

>

파이썬3




# Python 3 program to find> # factorial of given number> > def> factorial(n):> > ># single line to find factorial> >return> 1> if> (n>=>=> 1> or> n>=>=> 0>)>else> n>*> factorial(n>-> 1>)> > > # Driver Code> num>=> 5> print> (>'Factorial of'>, num,>'is'>,> >factorial(num))> > # This code is contributed> # by Smitha Dinesh Semwal.>

>

>

씨#




// C# program to find factorial> // of the given number> using> System;> > class> Factorial {> > >int> factorial(>int> n)> >> > >// Driver Code> >public> static> void> Main()> >{> >Factorial obj =>new> Factorial();> >int> num = 5;> > >Console.WriteLine(> >'Factorial of '> + num> >+>' is '> + obj.factorial(num));> >}> }> > // This code is contributed by vt_m.>

>

>

PHP




// PHP program to find factorial // of given number function factorial( $n) $n == 0) ? 1: $n * factorial($n - 1); // Driver Code $num = 5; echo 'Factorial of ', $num, ' is ', factorial($num); // This code is contributed by anuj_67. ?>>

>

>

자바스크립트




> > // JavaScript program to find factorial of given number> function> factorial(n)> > > // Driver Code> > >var> num = 5;> >document.write(>'Factorial of '> +num +>' is '> + factorial(num));> > // This code is contributed by shivanisinghss2110.> > >

>

>

산출

자바의 반환 유형
Factorial of 5 is 120>

시간 복잡도: 에)
보조 공간: 에)

팩토리얼 코드 작성 시 문제

n의 값이 1씩 증가하면 계승값도 n만큼 증가합니다. 따라서 팩토리얼 값을 저장하는 변수의 크기가 커야 합니다. 다음은 각 크기에 따라 계승이 저장될 수 있는 n 값입니다.

1. 정수 –> n<=12

2. long long int –> n<=19

위의 데이터를 보면 계승함수의 증가 속도가 빨라 매우 작은 n값도 계산할 수 있음을 알 수 있다. 그러나 각 단계에서 mod를 취함으로써 더 큰 값의 팩토리얼의 mod 값을 찾을 수 있습니다.

위의 해결 방법으로 인해 많은 수의 오버플로가 발생합니다. 큰 숫자에 적용되는 솔루션은 큰 숫자의 계승을 참조하세요.
위 코드/알고리즘에서 버그를 발견하거나 동일한 문제를 해결할 수 있는 다른 방법을 찾으면 의견을 작성해 주세요.