Approach:
(1) Take the average of the non-negative multiples of 3 which are less than or equal to 999.
(2) Multiply that by the count of the non-negative multiples of three which are less than or equal to 999
(3) Do the same for multiples of 5
(4) Do the same for multiples of (3 * 5 = 15)
(5) Add (2) and (3) and subtract (4) so as to avoid double-counting numbers which are multiples of both 3 and 5.
>>> def sum_of_factor(n):
... number_of_ints = 999//n + 1
... count_of_positive_ints = 999//n
... largest_positive_int = count_of_positive_ints * n
... average_int = (0 + largest_positive_int)/2.
... total_ints = (count_of_positive_ints + 1) * average_int
... return total_ints
...
>>> sum_of_factor(3)
166833.0
>>> sum_of_factor(5)
99500.0
>>> sum_of_factor(15)
33165.0
>>> sum_of_factor(3) + sum_of_factor(5) - sum_of_factor(15)
233168.0
No comments:
Post a Comment