Skip to content

Instantly share code, notes, and snippets.

@moski
Created May 20, 2012 17:18
Show Gist options
  • Save moski/2758803 to your computer and use it in GitHub Desktop.
Save moski/2758803 to your computer and use it in GitHub Desktop.
Return the number of odd numbers preceding a given number
# Return the number of odd numbers preceding a given number
# usage:
# puts Numberofoddnumbers::get(num)
# puts Numberofoddnumbers::get(num, 7) # will get all the odd numbers until 7
#
# blog post http://blog.moski.me/2012/05/drawing-hearts-in-ruby.html
#
module Numberofoddnumbers
def self.get(number, start_from=1)
sum = 0
for i in start_from..number do
sum = sum + 1 if i % 2 == 1
end
sum
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment