Python Bir Sayının Faktöriyelini Bulan Program

⚡ Akıllı Özet

Factorial of a number in Python is the product of every positive integer up to that number. Python computes it with a for loop, an if-else check, recursion, or the built-in math.factorial() function.

  • 🔘 Döngü için: Iterate from 1 to n, multiplying a running product to build the factorial.
  • ☑️ If-else check: Guard against negative input, returning an error since factorials need non-negative integers.
  • özyineleme: A function calls itself with n-1 until it reaches the base case of 0.
  • 🧪 math.factorial(): Import math and call one built-in function for a fast, tested result.
  • Sınır durumlar: The factorial of 0 equals 1, and Python handles very large results natively.
  • 🤖 AI and data science: Probability, combinatorics, and libraries like SciPy use factorials on arrays.

Python Bir Sayının Faktöriyelini Bulan Program

The sections below show four ways to compute a factorial in Python — a for loop, an if-else version, recursion, and math.factorial() — plus the underlying algorithm and applications.

Döngü için Kullanarak Bir Sayının Faktöriyeli

Örneği ele alalım Python code that takes a positive integer as input to determine the factorial of positive integers. In the following code, the loop begins with one, and then it multiplies by each number that precedes the actual number whose factorial is to be determined.

Aşağıdaki Python kod, bir döngü kullanarak faktöriyel fonksiyonu gösterir.

Python Kod:

print ("Input a number")
factorialIP = int (input ())
ffactor23 = 1
for j in range (1, factorialIP+1):
   ffactor23 = ffactor23 * j
print ("The factorial of the number is “, ffactor23)

Çıktı:

Input a number
4
The factorial of the number is 24

Yukarıdaki Python program takes the input of positive numbers only, and it does not have a check of negative numbers in it. In this program, the factor is 1 when j is equal to 1. When j is 2, the factor is multiplied with 2, and it will do the action till j comes at 4 to arrive at 24.

IF…else İfadesini Kullanarak Bir Sayının Faktöriyeli

Aşağıdaki Python code illustrates the factorial function using a function. Unlike the loop version, this program also checks for negative numbers before computing the factorial.

Önceki Python code, the check for negative numbers was not applied, making the factorial function incomplete and prone to deliver an error message if negative numbers are given as an input.

In the given code, the loop begins with one and multiplies by each preceding number, and the function also validates the input for negative numbers.

Python Kod:

print("Enter a number for the purpose of determining factorial")
factorialIP = int(input())
def factorial(factorialIP):
   if factorialIP < 0:
     print ('Factorial does not exist')
     factor=0
     return factor
   elif factorialIP == 0:
     factor=1
     return factor
     print(factor)
   else:
     factor = 1
     for j in range (1, factorialIP+1):
       factor = factor * j
     return factor
  print ("The factorial of the number is ", factorial(factorialIP))

Çıktı:

1) Enter a number to determine factorial
   -4
   Factorial does not exist
   The factorial of the number is 0

2) Enter a number to determine factorial
   4
   Factorial does not exist
   The factorial of the number is 24

Bu Python program accepts positive numbers and adds a check for negative numbers using the if and else statement, correctly returning 24 for an input of 4.

Özyinelemeyi Kullanarak Bir Sayının Faktöriyeli

Aşağıdaki Python code illustrates the factorial function using recursion. In this example, a recursive function that takes a positive integer as input determines the factorial number.

Python Kod:

print("Enter a number for the purpose of determining factorial")
def factorial(num2):
  if num2 < 0:
    return 'Factorial does not exist'
  elif num2 == 0:
     return 1
  else:
     return num2 * factorial(num2-1)
number1 = int(input())
print("The factorial of the number is",factorial(number1))

Çıktı:

Enter a number for the purpose of determining factorial
4
The factorial of the number is 24

Özyineleme, çağrılan işlevin olduğu bir kavram olarak açıklanabilir. Python module can call itself again and again. It runs until the Python condition present in the module is satisfied, wherein the function invoked is passed with a value.

Yukarıda Python program, the function def factorial keeps calling itself recursively until and unless the number reaches zero. Once the number reaches zero, it initializes the number as 1, ending the recursion.

Factorial of a Number using math.factorial()

Aşağıdaki Python kodu, math modülünü içe aktararak kullanılabilecek math.factorial() işlevini kullanarak faktöriyel fonksiyonunu gösterir.

This function does not accept negative integers, and it throws a value error when float numbers are provided.

Python Kod:

print("Enter a number for computing factorial")
import math
number1 = int(input())
print("The factorial is as computed comes out to be ")
print(math.factorial(number1))

Çıktı:

Enter a number for computing factorial
4
The factorial, as computed, comes out to be 24

Faktöriyel Programı Algoritması Python

Faktöriyel kavramını gösteren bir örnek ele alalım.

For determination of factorial 5, follow these steps:

5! = 5 x (5-1) x (5-2) x (5-3) x (5-4)
5! =120

İşte, 5! 120 olarak ifade edilir.

Aşağıdaki diyagram faktöriyel hesaplama algoritmasını anlamanıza yardımcı olacaktır. Bu durumda faktöriyel 4 örneğini ele alalım!

Faktöriyel Programı Algoritması

Algoritma ve faktöriyel 4'ün resimli örneği!

Faktöriyelin Uygulanması Python

Bir sayının faktöriyelinin matematikte geniş bir uygulama alanı vardır. İşte önemli uygulamalar Python:

  • Python hesaplamaya yardımcı olur ve bunu diğer mevcut programlama dillerine göre daha hızlı ve daha verimli bir şekilde yazdırma faktöriyeliyle takip eder.
  • MKS Python kod kolayca anlaşılabilir ve farklı platformlarda kopyalanabilir ve faktöriyel Python program çeşitli matematiksel model oluşturma ödevlerine dahil edilebilir.

SSS

The factorial of a non-negative integer n, written n!, is the product of all positive integers from 1 to n. For example, 5! equals 5 × 4 × 3 × 2 × 1 = 120.

By definition the factorial of 0 is 1. Every method — loop, recursion, or math.factorial() — returns 1 for input 0, and this base case also stops recursive calls.

Evet. Python integers have unlimited precision, so math.factorial(100) returns a 158-digit result without overflow. Only memory and running time grow with the input size.

A döngü için is usually preferred. Both run in O(n) time, but the loop uses constant memory, while recursion adds a call stack that can hit Python’s recursion limit.

Every standard method needs n−1 multiplications, so time complexity is O(n). The iterative loop keeps space at O(1), while recursion uses O(n) space for its call stack.

Deep recursion can exceed Python’s default limit near 1000 calls. Use an iterative for loop or math.factorial() for large inputs, or raise the ceiling with sys.setrecursionlimit().

Yes. Factorials appear in combinatorics and probability distributions like the Poisson and binomial that underpin many machine learning models. Data science libraries also offer vectorized factorial helpers for arrays.

Yes. GitHub Copilot and similar AI assistants generate loop, recursive, and math.factorial() versions from a short prompt. Always review the suggestion for the negative-number guard and the 0! base case.

Bu yazıyı şu şekilde özetleyin: