Co to jest silnia liczby?
- Silnia nieujemnej liczby całkowitej to mnożenie wszystkich dodatnich liczb całkowitych mniejszych lub równych n. Na przykład silnia liczby 6 to 6*5*4*3*2*1, czyli 720.
- Silnia jest reprezentowana przez liczbę i ! zaznacz na końcu. Jest szeroko stosowany w permutacjach i kombinacjach w celu obliczenia całkowitych możliwych wyników. Po raz pierwszy tego wykrzyknika użył francuski matematyk Christian Kramp.
Utwórzmy program silniowy, korzystając z funkcji rekurencyjnych. Dopóki wartość nie będzie równa zero, funkcja rekurencyjna wywoła samą siebie. Silnię można obliczyć za pomocą następującego wzoru rekurencyjnego.
N! = n * (n – 1)!
N! = 1, jeśli n = 0 lub n = 1
Poniżej implementacja:
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> |
>
foreach maszynopis
>
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;> }> |
>
>
Jawa
// 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> > > |
>
>Wyjście
Factorial of 5 is 120>
Złożoność czasowa: NA)
Przestrzeń pomocnicza: NA)
Rozwiązanie iteracyjne w celu znalezienia silni liczby:
Silnię można również obliczyć iteracyjnie, ponieważ rekurencja może być kosztowna w przypadku dużych liczb. Tutaj pokazaliśmy podejście iteracyjne, używając zarówno pętli for, jak i while.
Podejście 1: Korzystanie z pętli For
Postępuj zgodnie z instrukcjami, aby rozwiązać problem:
- Korzystając z pętli for, napiszemy program znajdujący silnię liczby.
- W programie zostanie użyta zmienna całkowita o wartości 1.
- Z każdą iteracją wartość będzie zwiększana o 1, aż będzie równa wartości wprowadzonej przez użytkownika.
- Silnia liczby wprowadzonej przez użytkownika będzie ostateczną wartością zmiennej faktu.
Poniżej implementacja powyższego podejścia:
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;> }> |
równość obiektów w Javie
>
>
Jawa
// 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.> > > |
>
>Wyjście
Factorial of 5 is 120>
Złożoność czasowa: NA)
Przestrzeń pomocnicza: O(1)
Podejście 2: W tym przykładzie zastosowano pętlę while do zaimplementowania algorytmu i znalezienia programu silni.
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> |
ciąg znaków w metodach Java
>
>
Jawa
// 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> > > > |
>
>Wyjście
Factorial of 5 is 120>
Złożoność czasowa: NA)
Przestrzeń pomocnicza: O(1)
Podejście 3: A operator trójskładnikowy można traktować jako skrót instrukcji if…else. Podano warunki wraz z instrukcjami, jakie należy na ich podstawie wykonać. Oto program do silni wykorzystujący operator trójskładnikowy.
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
testowanie kompatybilności
// 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.> |
>
>
Jawa
// 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.> > > |
>
>Wyjście
jak usunąć kolumnę w postgresql
Factorial of 5 is 120>
Złożoność czasowa: NA)
Przestrzeń pomocnicza: NA)
Problemy w pisaniu kodu silni
Gdy wartość n zmienia się o 1, wartość silni wzrasta o n. Zatem zmienna przechowująca wartość silni powinna mieć duży rozmiar. Poniżej znajduje się wartość n, której silnia może być przechowywana w odpowiednim rozmiarze.
1. liczba całkowita –> n<=12
2. long long int –> n<=19
Z powyższych danych widać, że ze względu na szybszy wzrost funkcji silni można obliczyć bardzo małą wartość n. Możemy jednak znaleźć wartość mod silni większych wartości, biorąc mod na każdym kroku.
Powyższe rozwiązanie powoduje przepełnienie w przypadku dużych liczb. Aby uzyskać rozwiązanie, które działa w przypadku dużych liczb, zapoznaj się z silnią dużej liczby.
Napisz komentarz, jeśli znajdziesz jakiś błąd w powyższym kodzie/algorytmie lub znajdź inne sposoby rozwiązania tego samego problemu.