From: Guillaume Marcais Date: 2005-03-28T21:46:31+09:00 Subject: Re: Convert a Hash into an Array "Mathieu Bouchard" schrieb im Newsbeitrag news:Pine.LNX.4.21.0503262110380.5161-100000@mondrian.artengine.ca... > > On Fri, 18 Mar 2005, Phrogz wrote: >> Aside - whatever the name, what an excellent, versatile function! For >> example, the linked-to geometric series sum at >> http://www.brpreiss.com/books/opus8/programs/pgm02_06.txt can be >> written in a more terse and (IMO) more ruby-esque way as simply: >> def geometric_series_sum( x, n ) >> (0..n).inject(0){ |sum,i| sum+x**i } >> end > > And even shorter, > > def geometric_series_sum(x,n) (x**(n+1)-1)/(x-1) end > To be very picky: def geometric_series_sum(x,n) (x==1) ? n+1 : (x**(n+1)-1)/(x-1) end But given how good the original implementation is (it calculates the power with a loop, restarting from 1 for every n...), almost anything is an improvement. Guillaume.