数値の階乗を計算するプログラム

数値の階乗を計算するプログラム

数値の階乗とは何ですか?

  • 非負の整数の階乗は、n 以下のすべての正の整数の乗算です。たとえば、6 の階乗は 6*5*4*3*2*1 となり、720 になります。
  • 階乗は数値と ! で表されます。最後にマークを付けます。これは、考えられる結果の合計を計算するために、順列や組み合わせで広く使用されています。フランスの数学者クリスチャン・クランプがこの感嘆符を初めて使用しました。

推奨される実践階乗 試してみましょう!

再帰関数を使用して階乗プログラムを作成してみましょう。値がゼロに等しくなるまで、再帰関数はそれ自体を呼び出します。階乗は次の再帰式を使用して計算できます。

ん! = n * (n – 1)!
ん! 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




// 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> ));> > }> }>

Python3




# 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#




// 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




> // 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>

C




#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;> }>

ジャワ




// 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> ));> > }> }>

Python3




# 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#




// 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




> // 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




// 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> ));> > }> }>

Python3




# 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#




// 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));> > }> }>

JavaScript




> > // 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




// 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.>

Python3




# 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#




// 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




> > // 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 増加します。したがって、factorial の値を格納する変数のサイズは大きくなければなりません。以下は、階乗をそれぞれのサイズに格納できる n の値です。

1. 整数 –> n <=12

2.long long int –> n <=19

上記のデータから、階乗関数の成長が速いため、非常に小さな値の n を計算できることがわかります。ただし、各ステップで mod を取得することで、より大きな値の階乗の mod 値を見つけることができます。

上記の解決策では、数値が大きい場合にオーバーフローが発生します。大きな数に有効な解決策については、大きな数の階乗を参照してください。
上記のコード/アルゴリズムにバグを見つけた場合、または同じ問題を解決する他の方法を見つけた場合は、コメントを書いてください。