Created
April 23, 2013 15:31
-
-
Save mjhea0/5444573 to your computer and use it in GitHub Desktop.
Find the sum of all the multiples of 5 or 7 below 100
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
# create a Proc to use with a Block | |
multiples = Proc.new do |n| | |
n % 5 == 0 || n % 7 == 0 | |
end | |
# turn the Proc into a Block | |
array = (1..99).to_a.select(&multiples) | |
# sum the members of the array | |
array.inject{|sum,x| sum + x } | |
# reuse the Proc (remember Procs are Objects, Blocks are not, so they can be used over and over) | |
array2 = (1..999).to_a.select(&multiples) | |
array2.inject{|sum,x| sum + x } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment