Just starting out in Python, coming from a C/C++ background. Any comments on style, how its solved, unseen redundancies, is it 'Pythonic' enough or am I lacking in the general syntax structure?
#https://projecteuler.net/problem=1
'''
problem statement: If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.
Find the sum of all the multiples of 3 or 5 below 1000.
Created on Jan 12, 2018
@author: Anonymous3.1415
'''
#is test number divisable by divisor
def divisable_by(divisor,test_num):
if not test_num % divisor:
return True
else:
return False
'''
FUNCTION DEF: nat_nums_divisors_1_or_2(divisor1, divisor2 , range_start, range_end)
PURPOSE: Find list of natural numbers that are divisable by either divisor1 or divisor2
@ var: (nat_nums_list) list of numbers that satisfy conditon
@ var: (nat_num_iterator) used to iterate through the natural numbers withing a given range
'''
def nat_nums_divisors_1_or_2(divisor1, divisor2 , range_start, range_end):
nat_nums_list = []
for nat_num_iterator in range(range_start, range_end):
if divisable_by(divisor1, nat_num_iterator) or divisable_by(divisor2, nat_num_iterator):
nat_nums_list.append(nat_num_iterator)
nat_num_iterator += 1
return nat_nums_list
nat_nums_div_3_5 = [] # Natural numbers divisable by either 3 or 5
divisor_3, divisor_5 = 3, 5
range_start, range_end = 1, 1000
nat_nums_div_3_5 = nat_nums_divisors_1_or_2(divisor_3, divisor_5, range_start, range_end)
nat_nums_div_3_5_sum = sum(nat_nums_div_3_5)
print ("The sum of Natural Numbers in range: [%d, %d]; divisable by either 3 or 5 is: %d") % (range_start, range_end, nat_nums_div_3_5_sum)
set(A) + set(B) - interaction(A, B)
. That's because in adding the two sets individually, you double count the items that belong in both, i.e., numbers divisible by 15. This is what Chris G in the comments and Alan Hoover's answer effectively do. \$\endgroup\$