def test_for_amicability(number, set_of_amicable): if number in set_of_amicable: return test_pair = sum(euler.iterator_divisors(number)) if test_pair in set_of_amicable or test_pair == number: return test_number = sum(euler.iterator_divisors(test_pair)) if test_number == number: set_of_amicable.add(number) set_of_amicable.add(test_pair)
def problem33(): fractions = find_curious_fractions() if len(fractions) != 4: raise ValueError('Expected four fractions') numerator = 1 denominator = 1 for f in fractions: numerator *= f[0] denominator *= f[1] print 'product of fractions is {0}/{1}'.format(numerator, denominator) divisors_numerator = set((x for x in euler.iterator_divisors(numerator, include_self=True))) divisors_denominator = sorted((x for x in euler.iterator_divisors(denominator, include_self=True))) divisor = 0 for x in reversed(divisors_denominator): if x in divisors_numerator: divisor = x break if divisor > 0: return denominator/divisor return denominator