Esempio n. 1
0
def sum_of_multiples(maximum, divisor_one, divisor_two):
    """
    Function returns sum of all multiples of divisor_one and divisor_two less
    than or equal to the maximum.

    Uses the principle of summation from 1 to n ( n(n+1)/2 ), therefore the sum of all multiples
    of k to m is ( km(m+1)/2 ), where m is the number of times k goes into the max (n).

    REQ: max, divisor_one, divisor_two >= 1
    REQ: type(max, divisor_one, divisor_two) == int

    :param maximum: {int} is maximum int to sum to
    :param divisor_one: {int} is first multiple to sum
    :param divisor_two: {int} is second multiple to sun
    :return: {int} sum of all multiples of divisor_one and divisor_two to maximum
    """

    # check input, raise relevant errors
    if type(maximum) != int or type(divisor_one) != int or type(
            divisor_two) != int:
        raise TypeError("One of input params was not type of int")
    if maximum <= 0 or divisor_one <= 0 or divisor_two <= 0:
        raise ValueError("One of input params was a negative/zero value")

    # get whole number of times each divisor goes into max
    quotient_one = maximum // divisor_one
    quotient_two = maximum // divisor_two

    # get lcm of divisor_one & divisor_two, as this will be double counted
    lcm = least_common_multiple(divisor_one, divisor_two)
    lcm_quotient = maximum // lcm

    # get divisor and lcm sums
    divisor_one_sum = divisor_one * summation(quotient_one)
    divisor_two_sum = divisor_two * summation(quotient_two)
    lcm_sum = lcm * summation(lcm_quotient)

    # add quotient sums and subtract lcm sum
    return divisor_one_sum + divisor_two_sum - lcm_sum
Esempio n. 2
0
 def test_input_one(self):
     self.assertEqual(least_common_multiple(11, 1), 11)
Esempio n. 3
0
 def test_input_zero(self):
     self.assertEqual(least_common_multiple(11, 0), 0)
Esempio n. 4
0
 def test_two_primes(self):
     self.assertEqual(least_common_multiple(11, 7), 77)
Esempio n. 5
0
 def test_one_prime(self):
     self.assertEqual(least_common_multiple(11, 4), 44)
Esempio n. 6
0
 def test_same_input(self):
     self.assertEqual(least_common_multiple(15, 15), 15)
Esempio n. 7
0
 def test_input_is_return(self):
     self.assertEqual(least_common_multiple(15, 3), 15)
Esempio n. 8
0
 def test_regular(self):
     self.assertEqual(least_common_multiple(4, 3), 12)