Kas ir skaitļa faktoriāls?
- Nenegatīva vesela skaitļa faktoriāls ir visu pozitīvo veselo skaitļu, kas ir mazāki vai vienādi ar n, reizinājums. Piemēram, koeficients 6 ir 6*5*4*3*2*1, kas ir 720.
- Faktoriālu apzīmē ar skaitli un ! beigās atzīmējiet. To plaši izmanto permutācijās un kombinācijās, lai aprēķinātu kopējos iespējamos rezultātus. Franču matemātiķis Kristians Kramps vispirms izmantoja izsaukumu.
Izveidosim faktoriālu programmu, izmantojot rekursīvās funkcijas. Kamēr vērtība nav vienāda ar nulli, rekursīvā funkcija izsauks sevi. Faktoriālu var aprēķināt, izmantojot šādu rekursīvo formulu.
n! = n * (n – 1)!
n! = 1, ja n = 0 vai n = 1
Tālāk ir norādīta ieviešana:
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> |
>
tkinter poga
>
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 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>
>
>
>>IzvadeFactorial of 5 is 120>Laika sarežģītība: O(n)
Palīgtelpa: O(n)Iteratīvs risinājums, lai atrastu skaitļa faktoriālu:
Faktoriālu var aprēķināt arī iteratīvi, jo lieliem skaitļiem rekursija var būt dārga. Šeit mēs esam parādījuši iteratīvo pieeju, izmantojot gan for, gan while cilpas.
1. pieeja: Izmantojot For cilpu
Lai atrisinātu problēmu, veiciet tālāk norādītās darbības.
- Izmantojot for cilpu, mēs uzrakstīsim programmu skaitļa faktoriāla atrašanai.
- Programmā tiks izmantots vesels mainīgais ar vērtību 1.
- Katrā iterācijā vērtība palielināsies par 1, līdz tā būs vienāda ar lietotāja ievadīto vērtību.
- Lietotāja ievadītā skaitļa faktoriāls būs fakta mainīgā galīgā vērtība.
Tālāk ir norādīta iepriekš minētās pieejas īstenošana.
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
izmēģiniet datu struktūru
#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 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.>
>
>
>>IzvadeFactorial of 5 is 120>Laika sarežģītība: O(n)
Palīgtelpa: O(1)2. pieeja: Šajā piemērā algoritma ieviešanai un faktoriālās programmas atrašanai tiek izmantota kamēr cilpa.
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++
novirzīšanas rādītājs
// 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#
// 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));>
>
}>
}>
python inicializācijas saraksts>>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>
>
>
>
>>IzvadeFactorial of 5 is 120>Laika sarežģītība: O(N)
Palīgtelpa: O(1)3. pieeja: A trīskāršs operators var uzskatīt par īsu frāzi paziņojumam if…else. Nosacījumi ir sniegti kopā ar paziņojumiem, kas jāizpilda, pamatojoties uz tiem. Šeit ir programma faktoriālam, izmantojot trīskāršu operatoru.
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 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.>
>
>
>>Izvadereactjs karteFactorial of 5 is 120>Laika sarežģītība: O(n)
Palīgtelpa: O(n)Problēmas ar faktoriāla koda rakstīšanu
Kad n vērtība mainās par 1, faktoriāla vērtība palielinās par n. Tātad mainīgajam, kurā tiek saglabāta faktoriāla vērtība, jābūt lielam. Tālāk ir norādīta n vērtība, kuras faktoriālu var saglabāt attiecīgajā izmērā.
1. vesels skaitlis –> n<=12
2. garš garš int –> n<=19
No iepriekš minētajiem datiem var redzēt, ka var aprēķināt ļoti mazu n vērtību, jo faktoriālā funkcija ir ātrāka. Tomēr mēs varam atrast lielāku vērtību faktoriāla mod vērtību, veicot mod katrā solī.
Iepriekš minētais risinājums rada pārplūdi lielam skaitam. Lai atrastu risinājumu, kas darbojas lieliem skaitļiem, lūdzu, skatiet lielu skaitļu faktoriālu.
Lūdzu, rakstiet komentārus, ja atrodat kādu kļūdu iepriekš minētajā kodā/algoritmā vai atrodiet citus veidus, kā atrisināt to pašu problēmu.