Ruby:最大公因数/最小公倍数

清华大佬耗费三个月吐血整理的几百G的资源,免费分享!....>>>

print "Enter one number:"
n1 = gets.to_i
print "Enter another number:"
n2 = gets.to_i
gcf = 0
lcm = 0
if n1 >= n2
dividend = n1
divisor = n2
else
dividend = n2
divisor = n1
end
# Uses the euclidian algorithm to find the GCF
  def gcf_finder(dividend, divisor)
  remainder = -1
   while remainder != 0
    quotient = dividend / divisor
    remainder = dividend % divisor
     if remainder != 0
  	   dividend = divisor
  	   divisor = remainder
     end
    gcf = divisor
   return divisor
   end
  end

# Gets the least common multiple
  def lcm_finder(n1, n2, gcf)
   lcm = (n1 * n2) / gcf
   return lcm
  end
  
gcf = gcf_finder(dividend, divisor)
lcm = lcm_finder(n1, n2, gcf)
print "The greatest common factor of the given numbers is #{gcf}\n"
lcm = n1 * n2/gcf
print "The least common muliple is #{lcm}\n"