public
Created

  • Download Gist
gistfile1.ru
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
def geom_sequence c
Enumerator.new { |y|
a = 1
loop {
y << a
a *= c
}
}
end
 
g = geom_sequence 2
p g.take_while{|i| i < 100}
#=>[1, 2, 4, 8, 16, 32, 64]
g = geom_sequence 0.5
p g.take(10)
#=>[1, 0.5, 0.25, 0.125, 0.0625, 0.03125, 0.015625, 0.0078125, 0.00390625, 0.001953125]

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.