RosettaCodeData/Task/Jensens-Device/Ruby/jensens-device-3.rb

7 lines
241 B
Ruby
Raw Permalink Normal View History

2015-11-18 06:14:39 +00:00
def sum lo, hi, &term
2019-09-12 10:33:56 -07:00
(lo..hi).sum(&term)
2015-11-18 06:14:39 +00:00
end
2019-09-12 10:33:56 -07:00
p sum(1,100){|i| 1.0/i} # => 5.187377517639621
2015-11-18 06:14:39 +00:00
# or using Rational:
2019-09-12 10:33:56 -07:00
p sum(1,100){|i| Rational(1,i)} # => 14466636279520351160221518043104131447711 / 2788815009188499086581352357412492142272