brewersfan1976

factorizedGCD.rb

May 18th, 2016
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 0.76 KB | None | 0 0
  1. def factorizedGCD(a, b)
  2.     a_result = 1
  3.     b_result = 1
  4.     x = 0
  5.     smallest_number = 0
  6.     gcd = 0
  7.    
  8.     a_array_length = a.length
  9.     b_array_length = b.length
  10.    
  11.     while (x < a_array_length)
  12.         a_result = a_result * a[x]
  13.         x = x + 1
  14.     end
  15.    
  16.     x = 0
  17.    
  18.     while (x < b_array_length)
  19.         b_result = b_result * b[x]
  20.         x = x + 1
  21.     end
  22.    
  23.     x = 1
  24.    
  25.     if a_result <= b_result then
  26.         smallest_number = a_result
  27.     else
  28.         smallest_number = b_result
  29.     end
  30.        
  31.     while (x <= smallest_number)
  32.           if (a_result % x == 0) and (b_result % x == 0) then
  33.              gcd = x
  34.           end
  35.                
  36.           x = x + 1
  37.     end
  38.        
  39.     return gcd
  40.    
  41. end
Advertisement
Add Comment
Please, Sign In to add comment