Skip to content

Instantly share code, notes, and snippets.

@LSStaff
Created January 16, 2018 06:21
Show Gist options
  • Save LSStaff/0800cf6fe0a95980921885d39b07462b to your computer and use it in GitHub Desktop.
Save LSStaff/0800cf6fe0a95980921885d39b07462b to your computer and use it in GitHub Desktop.
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