Created
May 20, 2012 17:18
-
-
Save moski/2758803 to your computer and use it in GitHub Desktop.
Return the number of odd numbers preceding a given number
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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