예제 #1
0
def main():
    # Determine all of the abundant integers in this range.
    # Python sets are much faster for lookups than lists.
    abundant = set(n for n in range(1, 28124) if sum(divisors(n)) > n)

    print(
        sum(n for n in range(1, 28124)
            if not any(n - i in abundant for i in abundant)))
예제 #2
0
def sum_of_amicable_numbers(max_n):
    n_to_sum = {}
    for n in range(1, max_n + 1):
        sum_of_divisors = sum(util.divisors(n))
        n_to_sum[n] = sum_of_divisors
    amicable = (n for n, sod in n_to_sum.iteritems()
                if n_to_sum.get(sod, 0) == n and n != sod)
    return sum(amicable)
예제 #3
0
def main():
    sums = {}
    amicable = []

    for n in range(30000):
        sums[n] = sum(divisors(n))

    for n in range(10000):
        if sums[sums[n]] == n and sums[n] != n:
            amicable.append(n)

    print(sum(amicable))
예제 #4
0
파일: 023.py 프로젝트: jag426/euler
from util import divisors

limit = 28124
abundant = lambda n: sum(divisors(n)) > n

non_abundant_sums = set(range(1, limit))
abundants = set()
for n in range(1, limit):
    if abundant(n):
        abundants.add(n)
        for a in abundants:
            non_abundant_sums.discard(n+a)
print(sum(non_abundant_sums))
예제 #5
0
파일: 23.py 프로젝트: benjamn/project-euler
def abundant(n):
    return sum(divisors(n)) > n
예제 #6
0
def amicable_pair(a):
  b = sum(util.divisors(a))
  return (a, b) if a != b and sum(util.divisors(b)) == a else None
from util import divisors

count = 1
n = 0

while 1:
    n += count
    count += 1
    if(len(divisors(n)) > 500):
        print n
        exit()
예제 #8
0
'''
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 n is called deficient if the sum of its proper divisors is less than n and it is called abundant if this sum exceeds n.

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.
'''
from util import divisors

N = 28123
# N = 100
abundants = []
for i in xrange(1, N):
    ds = divisors(i)
    if sum(ds)-i > i:
        abundants.append(i)
# print abundants
# print len(abundants)

non_abundants = [True] * N
for i in xrange(0, len(abundants)):
    for j in xrange(i, len(abundants)):
        tmp = abundants[i]+abundants[j]
        if tmp < N:
            non_abundants[tmp] = False

total = 0
for i in xrange(1, N):
    if non_abundants[i]:
예제 #9
0
from util import divisors

# Lowest number is 20162 according to wolfram alpha
abundant = [x for x in xrange(20162) if sum(divisors(x))-x>x]

sums = set([])
for i in range(len(abundant)):
    for j in range(i, len(abundant)):
        h = abundant[i] + abundant[j]
        if h > 20162: break
        sums.add(h)

print sum( set(i for i in range(20162)) - sums )
예제 #10
0
파일: 21.py 프로젝트: benjamn/project-euler
from util import memo, divisors, amicable
from sys import argv

for i in xrange(int(argv[1])):
    if amicable(i):
        print i, sum(divisors(i))