looking for help here!
Does anyone know how to convert the recursive version of Euclid's algorithm (to find the greatest common divisor GCD) into a version that uses STACK?
Here is the recursive version of Euclid's algorithm:
def euclid_gcd(a, b):
if a == 0:
return b
return gcd(b%a, a)
Right now I have a starting code to convert to a STACK version of Euclid's algorithm:
def euclid_gcd_stack(a, b):
s = Stack()
s.push(a)
s.push(b)
while s.count() > 0:
b = s.pop()
a = s.pop()
<code to be continued here>
Thank you in advance: :)
def euclid_gcd_stack(a, b):
s = Stack()
s.push(a)
s.push(b)
while s.count() > 0:
b = s.pop()
a = s.pop()
if a > b:
a, b = b, a
if a == 0:
return b
else:
s.push(a)
s.push(b - a)
``
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.