Epic - Decimal Number

Let the user enter a decimal number. Therange allowed is 0.0001 to 0.9999. Only four decimal places are allowed. Theoutput should be an irreducible fraction. E.g.: If the user enters 0.35,the irreducible fraction will be 7/20.

等于找与10000的公约数

def fraction(d)
  x = d*10000
  gcd = findgcd(x,10000)
  (x/gcd).to_i.to_s + '/' + (10000/gcd).to_i.to_s
end

def findgcd(a,b)
  return a if b == 0
  findgcd(b,a%b)
end

 

posted @ 2015-06-13 08:47  lilixu  阅读(163)  评论(0编辑  收藏  举报