title: Using stack to find the greatest common divisor
body: <p>I am trying to implement an alogorithm to find the greatest common divisor using a stack: I am unable to formulate the correct answer based on my logic below. Please help. Here is my code:</p>
  
  <pre><code>def d8(a,b)
  if (a==b)
      return a 
  end
  s = Stack.new
  s.push(b)
  s.push(a)
  
  c1 = s.pop
  c2 = s.pop
  
  while c1!=c2
      if s.count&gt;0
          c1 = s.pop
          c2 = s.pop
      end
  
      if c1== c2
          return c1
      elsif c1&gt;c2
          c1 = c1-c2
          s.push(c2)
          s.push(c1)
      else
          c2 = c2 -c1
          s.push(c2)
          s.push(c1)
      end
  end
      return nil
  end
  </code></pre>