Python एल्गोरिदम

Python में Recursive Factorial

Formula n! = n × (n−1)! base case 0 या 1 पर रुकता है।

में Recursive Factorial क्या है?

Formula n! = n × (n−1)! base case 0 या 1 पर रुकता है।

Recursive function से n! निकालें।

इसका उपयोग कब करें?

  • Algorithms और data structures की कार्यप्रणाली समझें।
  • हर चरण देखें और edge cases जाँचें।
  • समय और memory की जटिलता की तुलना करें।

उदाहरण कोड

कोड चलाएँ →
main.py
def factorial(number):
    if number < 0:
        raise ValueError("Factorial is undefined for negative numbers")
    if number <= 1:
        return 1
    return number * factorial(number - 1)

print(factorial(6))

अपेक्षित आउटपुट

720

यह कैसे काम करता है

हर call n घटाता है; समय और call-stack depth दोनों O(n) हैं।

मान बदलें और Python इंस्टॉल किए बिना CodeUtility ऑनलाइन Python कंपाइलर में प्रोग्राम चलाएँ।

अभ्यास के कार्य

बड़े dataset पर जाने से पहले input बदलें और edge cases की जाँच करें।

  1. खाली input, एक element और duplicate values जाँचें।
  2. हर चरण के बाद state दिखाएँ।
  3. Performance को दूसरे solution से तुलना करें।
Python IDE में चलाएँ →