Python program za pronalaženje faktorijela broja
Što je faktorijel?
Faktorijel je nenegativan cijeli broj. To je umnožak svih pozitivnih cijelih brojeva manjih ili jednakih broju koji tražite za faktorijel. Označava se znakom uzvika (!).
Primjer:
n! = n* (n-1) * (n-2) *........1 4! = 4x3x2x1 = 24
Faktorijelna vrijednost 4 je 24.
Napomena: Faktorijelna vrijednost 0 uvijek je 1. (kršenje pravila)
Primjer -
num = int(input('Enter a number: ')) factorial = 1 if num <0: 0 print(' factorial does not exist for negative numbers') elif num="=" 0: print('the of is 1') else: i in range(1,num + 1): of',num,'is',factorial) < pre> <p> <strong>Output:</strong> </p> <pre> Enter a number: 10 The factorial of 10 is 3628800 </pre> <p> <strong>Explanation -</strong> </p> <p>In the above example, we have declared a <strong>num</strong> variable that takes an integer as an input from the user. We declared a variable factorial and assigned 1. Then, we checked if the user enters the number less than one, then it returns the factorial does not exist for a negative number. If it returns false, then we check num is equal to zero, it returns false the control transfers to the else statement and prints the factorial of a given number.</p> <h3>Using Recursion</h3> <p>Python recursion is a method which calls itself. Let's understand the following example.</p> <p> <strong>Example -</strong> </p> <pre> # Python 3 program to find # factorial of given number def fact(n): return 1 if (n==1 or n==0) else n * fact(n - 1); num = 5 print('Factorial of',num,'is',) fact(num)) </pre> <p> <strong>Output:</strong> </p> <pre> Factorial of 5 is 120 </pre> <p> <strong>Explanation -</strong> </p> <p>In the above code, we have used the recursion to find the factorial of a given number. We have defined the <strong>fact(num)</strong> function, which returns one if the entered value is 1 or 0 otherwise until we get the factorial of a given number.</p> <h3>Using built-in function</h3> <p>We will use the math module, which provides the built-in <strong>factorial()</strong> method. Let's understand the following example.</p> <p> <strong>Example -</strong> </p> <pre> # Python program to find # factorial of given number import math def fact(n): return(math.factorial(n)) num = int(input('Enter the number:')) f = fact(num) print('Factorial of', num, 'is', f) </pre> <p> <strong>Output:</strong> </p> <pre> Enter the number: 6 Factorial of 6 is 720 </pre> <p>We have imported the math module that has <strong>factorial()</strong> function. It takes an integer number to calculate the factorial. We don't need to use logic.</p> <hr></0:> Objašnjenje -
U gornjem primjeru, proglasili smo a na jedan varijabla koja uzima cijeli broj kao unos od korisnika. Deklarirali smo varijablu faktorijel i dodijelili joj 1. Zatim smo provjerili da li korisnik unese broj manji od jedan, a zatim vraća faktorijel ne postoji za negativan broj. Ako vrati false, tada provjeravamo da je num jednak nuli, vraća false kontrola se prenosi na naredbu else i ispisuje faktorijel danog broja.
Korištenje rekurzije
Python rekurzija je metoda koja poziva samu sebe. Razumimo sljedeći primjer.
Primjer -
# Python 3 program to find # factorial of given number def fact(n): return 1 if (n==1 or n==0) else n * fact(n - 1); num = 5 print('Factorial of',num,'is',) fact(num))
Izlaz:
Factorial of 5 is 120
Objašnjenje -
U gornjem kodu upotrijebili smo rekurziju za pronalaženje faktorijela zadanog broja. Definirali smo činjenica funkcija koja vraća jedan ako je unesena vrijednost 1 ili 0 inače dok ne dobijemo faktorijel zadanog broja.
Korištenje ugrađene funkcije
Koristit ćemo matematički modul koji nudi ugrađeni faktorijel() metoda. Razumimo sljedeći primjer.
Primjer -
# Python program to find # factorial of given number import math def fact(n): return(math.factorial(n)) num = int(input('Enter the number:')) f = fact(num) print('Factorial of', num, 'is', f)
Izlaz:
Enter the number: 6 Factorial of 6 is 720
Uvezli smo matematički modul koji ima faktorijel() funkcija. Za izračun faktorijela potreban je cijeli broj. Ne trebamo koristiti logiku.
0:>