Created
January 16, 2018 06:21
-
-
Save LSStaff/0800cf6fe0a95980921885d39b07462b to your computer and use it in GitHub Desktop.
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
def sum_of_multiples(target, factors) | |
multiples = [] | |
factors = [3, 5] if factors.length == 0 | |
factors.each do |factor| | |
current_multiple = factor | |
while current_multiple < target | |
multiples << current_multiple | |
current_multiple += factor | |
end | |
end | |
multiples.uniq.inject(0, :+) | |
end | |
sum_of_multiples(20, [3, 5]) # returns 78 | |
sum_of_multiples(20, [3]) # returns 63 | |
sum_of_multiples(20, [5]) # returns 30 | |
sum_of_multiples(20, []) # returns 78 | |
sum_of_multiples(1, []) # returns 0 | |
sum_of_multiples(20, [19]) # returns 19 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment