Python एल्गोरिदम

Python में Euclidean Algorithm

Algorithm बार-बार जोड़ी को divisor और remainder से बदलता है।

में Euclidean Algorithm क्या है?

Algorithm बार-बार जोड़ी को divisor और remainder से बदलता है।

दो numbers का greatest common divisor निकालें।

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

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

उदाहरण कोड

कोड चलाएँ →
main.py
def gcd(first, second):
    while second:
        first, second = second, first % second
    return abs(first)

print(gcd(48, 18))

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

6

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

Remainder zero होने पर दूसरा value GCD होता है; समय जटिलता logarithmic है।

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

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

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

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