title: Python: Finding GCD using Middle School Procedure
body: <p>Middle School Procedure <code>GCD</code></p>
  
  <ul>
  <li>Step 1 Find the prime factors of m.</li>
  <li>Step 2 Find the prime factors of n.</li>
  <li>Step 3 Identify all the common factors in the two prime expansions
  found in <code>Step 1</code> and <code>Step 2</code>. (If p is a common factor occurring pm and
  pn timesin m and n, respectively, it should be repeated min{pm, pn}
  times.)</li>
  <li>Step 4 Compute the product of all the common factors and return it as
  the greatest common divisor of the numbers given.</li>
  </ul>
  
  <p>Thus, for the numbers 60 and 24, we get</p>
  
  <p><code>60 = 2 . 2 . 3 . 5</code></p>
  
  <p><code>24 = 2 . 2 . 2 . 3</code></p>
  
  <p><code>gcd(60, 24) = 2 . 2 . 3 = 12.</code></p>
  
  <p>So using the above instructions, this is what I got so far:</p>
  
  <pre><code>import numpy as np
  
  #find prime factors of m and output it to list fm
  def Middle(m,n):
      c = 2
      fm = [ ]  
      while m &gt; 1:
        if m % c == 0:
          fm.append(c)
          m = m/c
        else:
          c = c + 1             
      return fm
  
  #find prime factors of n and output it to list fn
      d = 2
      fn = [ ]  
      while n &gt; 1:
        if n % d == 0:
          fn.append(d)
          n = n/d
        else:
          d = d + 1 
      return fn
  
  #compare fm and fn and multiply common items
  #this is the part where I got wrong       
      cf = []
      for f in fm:
          if f in fn:
              cf.append(f) 
      return (np.prod(cf))
  </code></pre>
  
  <p>I know the last part is wrong but I have no idea how to fix it. The instructions said something about repeating the f to a minimum but I'm clueless. Please help.</p>