+ 3

Ruby array sum?

I am attempting to use what I have learned so far in the practice section but I am stuck. I need to find #s below a given number that are either divisible by 3 or 5, then those numbers added together for the sum. I'm getting the divisible numbers just fine, say if I input 10 then my result will be [3, 5, 6, 9], but I cannot for the life of me figure out how to take that array created & sum it. Edit: Also if a # is divisble by both 3 & 5, it doesn't appear twice in the array. Need to be able to plug any number in. Can anybody help point me in the right direction? Much appreciated!! https://code.sololearn.com/cNgclGps6BcU/?ref=app

14th Jun 2022, 2:18 AM
Emily Wood
Emily Wood - avatar
1 ответ
+ 2
Like this? def sum_multiples(num) mult = [] i = 1 while i < num if (i % 3 == 0) mult << i end if (i % 5 == 0) mult << i end i += 1 end return mult.sum end print sum_multiples(10)
16th Jul 2022, 5:40 PM
Paul
Paul - avatar