0
Could someone please help with this task?
Define a method, that sums all the numbers less than n that are: Divisible by 2 or divisible by 3 Not divisible by both 2 and 3
1 Answer
0
Ok, I will write this in Python since i don't know Ruby and python has a very similar syntax to Ruby. so you may get a grip of it..
so as far as I understood the method/function will give a sum of numbers (from a list ? ) that are divisible by 2 or 3 and not both.
def sum_of_num(nums):
# a list with numbers
# example: nums = [1,4,2,5,6,7,9,55,44,33,11]
# a variable to sum up all the possible numbers
sum_of_numbers = 0
# a loop to iterate through the list, called numbers
for num in nums:
# if (num is div by 2 or 3) and (num is not div by 2 AND 3 together)
if (num % 2 == 0 or num % 3 == 0) and not (num % 2 == 0 and num % 3 == 0):
# sum_of_numbers = sum_of_numbers + num
sum_of_numbers += num
return sum_of_numbers
print sum_of_num([1,4,2,5,6,7,9,55,44,33,11])