def problem12(): """ The sequence of triangle numbers is generated by adding the natural numbers. So the 7^(th) triangle number would be 1 + 2 + 3 + 4 + 5 + 6 + 7 = 28. The first ten terms would be: 1, 3, 6, 10, 15, 21, 28, 36, 45, 55, ... Let us list the factors of the first seven triangle numbers: We can see that 28 is the first triangle number to have over five divisors. What is the value of the first triangle number to have over five hundred divisors? """ def gen_triangle_num(): i = 1 num = 0 while True: num += i i += 1 yield num primelist = mlib.prime_sieve(10**6, output=[]) generator = gen_triangle_num() while True: num = generator.next() if len(mlib.get_factors(num, primelist)) > 500: return num
def problem21(): """ Let d(n) be defined as the sum of proper divisors of n (numbers less than n which divide evenly into n). If d(a) = b and d(b) = a, where a # b, then a and b are an amicable pair and each of a and b are called amicable numbers. For example, the proper divisors of 220 are 1, 2, 4, 5, 10, 11, 20, 22, 44, 55 and 110; therefore d(220) = 284. The proper divisors of 284 are 1, 2, 4, 71 and 142; so d(284) = 220. Evaluate the sum of all the amicable numbers under 10000. """ primelist = mlib.prime_sieve(10 ** 4, output=[]) amicable_map = {} am_sum = 0 for i in range(1, 10 ** 4 + 1): factors = mlib.get_factors(i, primelist) amicable_map[i] = sum(factors[:-1]) for i in amicable_map: if amicable_map[i] != i and amicable_map[i] in amicable_map and amicable_map[amicable_map[i]] == i: am_sum += i return am_sum
def problem23(): """ A perfect number is a number for which the sum of its proper divisors is exactly equal to the number. For example, the sum of the proper divisors of 28 would be 1 + 2 + 4 + 7 + 14 = 28, which means that 28 is a perfect number. A number whose proper divisors are less than the number is called deficient and a number whose proper divisors exceed the number is called abundant. As 12 is the smallest abundant number, 1 + 2 + 3 + 4 + 6 = 16, the smallest number that can be written as the sum of two abundant numbers is 24. By mathematical analysis, it can be shown that all integers greater than 28123 can be written as the sum of two abundant numbers. However, this upper limit cannot be reduced any further by analysis even though it is known that the greatest number that cannot be expressed as the sum of two abundant numbers is less than this limit. Find the sum of all the positive integers which cannot be written as the sum of two abundant numbers. """ primelist = mlib.prime_sieve(10 ** 5, output=[]) abundant = [] for i in range(1, 28123 + 1): factors = mlib.get_factors(i, primelist) if sum(factors[:-1]) > i: abundant.append(i) ab_sum = {} ret_sum = 0 for i in range(0, len(abundant)): if abundant[i] > 28123: break for j in range(0, len(abundant)): ab_sum[abundant[i] + abundant[j]] = 1 if abundant[i] + abundant[j] > 28123: break for i in range(1, 28123 + 1): if i not in ab_sum: ret_sum += i return ret_sum