brewersfan1976

gcpd.rb

Feb 22nd, 2016
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 0.90 KB | None | 0 0
  1. def greatestCommonPrimeDivisor(a, b)
  2.     x = 1
  3.     largest_prime = -1
  4.     min = 0
  5.     y = 1
  6.     prime = false
  7.    
  8.     if a <= b then
  9.        min = a
  10.     else
  11.         min = b
  12.     end
  13.        
  14.     while (x <= min)
  15.         if a % x == 0 and b % x == 0 then
  16.             while (y <= x)
  17.                 if x % y != 0 and y != 1 and x != y then
  18.                     prime = true
  19.                 end
  20.                    
  21.                 if x % y == 0 and y != 1 and x != y then
  22.                     prime = false
  23.                     break
  24.                 end
  25.                        
  26.                 y = y + 1
  27.               end
  28.                    
  29.               if prime == true then
  30.                  largest_prime = x
  31.               end
  32.           end        
  33.          
  34.           prime = false
  35.           y = 1
  36.           x = x + 1
  37.       end
  38.                
  39.       return largest_prime
  40. end
Advertisement
Add Comment
Please, Sign In to add comment