logo

Program za faktorijel števila

Kaj je faktorijel števila?

  • Faktoriel nenegativnega celega števila je množenje vseh pozitivnih celih števil, manjših ali enakih n. Na primer, faktoriel 6 je 6*5*4*3*2*1, kar je 720.
  • Faktoriel je predstavljen s številom in ! označite na koncu. Široko se uporablja v permutacijah in kombinacijah za izračun skupnih možnih rezultatov. Francoski matematik Christian Kramp je prvi uporabil vzklik.

Priporočena praksa Factorial Poskusite!

Ustvarimo faktorski program z uporabo rekurzivnih funkcij. Dokler vrednost ni enaka nič, bo rekurzivna funkcija klicala samo sebe. Faktoriel je mogoče izračunati z naslednjo rekurzivno formulo.

n! = n * (n – 1)!
n! = 1, če je n = 0 ali n = 1



Spodaj je izvedba:

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>

>

drevesni zemljevid
>

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




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

>

>

Izhod

Factorial of 5 is 120>

Časovna zapletenost: O(n)
Pomožni prostor: O(n)

Iterativna rešitev za iskanje faktoriala števila:

Faktoriel je mogoče izračunati tudi iterativno, saj je rekurzija lahko draga za velika števila. Tukaj smo prikazali iterativni pristop z uporabo zank for in while.

Pristop 1: Uporaba zanke For

Sledite korakom za rešitev težave:

  • S pomočjo zanke for bomo napisali program za iskanje faktoriala števila.
  • V programu bo uporabljena celoštevilska spremenljivka z vrednostjo 1.
  • Z vsako ponovitvijo se bo vrednost povečala za 1, dokler ne bo enaka vrednosti, ki jo je vnesel uporabnik.
  • Faktoriel števila, ki ga vnese uporabnik, bo končna vrednost v spremenljivki dejstva.

Spodaj je izvedba za zgornji pristop:

C++


iskalni algoritmi



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




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

>

>

Izhod

Factorial of 5 is 120>

Časovna zapletenost: O(n)
Pomožni prostor: O(1)

Pristop 2: Ta primer uporablja zanko while za implementacijo algoritma in iskanje faktorskega programa.

C


niz nizov v 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




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


ups koncepti



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

>

>

Izhod

Factorial of 5 is 120>

Časovna zahtevnost: O(N)
Pomožni prostor: O(1)

Pristop 3: A ternarni operater si lahko predstavljamo kot okrajšavo za izjavo if… else. Podani so pogoji in izjave, ki jih je treba na podlagi njih izvršiti. Tukaj je program za faktoriel z uporabo ternarnega operaterja.

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




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

>

>

Izhod

okno.odpri javascript
Factorial of 5 is 120>

Časovna zahtevnost: O(n)
Pomožni prostor: O(n)

Težave pri pisanju faktorialne kode

Ko se vrednost n spremeni za 1, se vrednost faktoriela poveča za n. Torej mora imeti spremenljivka, ki shranjuje vrednost faktoriala, veliko velikost. Sledi vrednost n, katere faktorial je mogoče shraniti v ustrezni velikosti.

1. celo število –> n<=12

2. dolgo dolgo int –> n<=19

Iz zgornjih podatkov lahko vidimo, da je mogoče izračunati zelo majhno vrednost n zaradi hitrejše rasti faktorialne funkcije. Vendar pa lahko najdemo mod vrednost faktoriala večjih vrednosti tako, da pri vsakem koraku vzamemo mod.

Zgornja rešitev povzroči prelivanje za velika števila. Za rešitev, ki deluje za velika števila, si oglejte faktoriel velikega števila.
Prosimo, napišite komentarje, če najdete kakšno napako v zgornji kodi/algoritmu, ali poiščite druge načine za rešitev iste težave.