Skip to content

Instantly share code, notes, and snippets.

@jablan
Created April 13, 2010 23:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jablan/365235 to your computer and use it in GitHub Desktop.
Save jablan/365235 to your computer and use it in GitHub Desktop.
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]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment