Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env ruby
- def repeating_decimal(a, b)
- x, r = a.divmod b
- qs = []
- rs = [r]
- loop {
- q, r = (r * 10).divmod b
- qs << q
- break if r == 0
- if i = rs.index(r)
- qs.insert i, '('
- qs << ')'
- break
- else
- rs << r
- end
- }
- "#{x}.#{qs.join ''}"
- end
- [*2..19].each {|e| puts "1/#{e} = #{repeating_decimal(1, e)}" }
- [[2, 3], [9, 11], [7, 12], [22, 7]].each {|e1, e2|
- puts "#{e1}/#{e2} = #{repeating_decimal(e1, e2)}"
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement