brewersfan1976

least_common_prime_divisor.rb

Jul 14th, 2016
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 0.36 KB | None | 0 0
  1. def leastCommonPrimeDivisor(a, b)
  2.     require 'prime'
  3.    
  4.     if a < b then
  5.        min = a
  6.     else
  7.        min = b
  8.     end
  9.        
  10.     x = 2
  11.        
  12.     while (x <= min)
  13.           if x.prime? == true and a % x == 0 and b % x == 0 then
  14.              return x
  15.           end
  16.              
  17.           x = x + 1
  18.     end
  19.        
  20.     return -1
  21. end
Advertisement
Add Comment
Please, Sign In to add comment