Problem 23

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.

Solution

def sum_divisors(n, divisor):
    sqrtN = n**0.5    
    i = divisor
    t = 0
    while i <= sqrtN:
        if not n%i:
            divisor = i
            if i == n//i or i == 1:
                t+= i
                break
            t += sum([i,n//i])
            break 
        i += 1
    if i < sqrtN:
        divisor += 1
        t += sum_divisors(n, divisor)
    return t

abund = []
abundSum = []
for i in range(1, 28124):
    if sum_divisors(i, 1) > i:
        abund += [i]
        for j in abund:
            if i+j > 28123:
                break
            abundSum += [i+j]
        
print(sum(range(1,28124)) - sum(set(abundSum)))
###################
##  END
###################
from time import time
st = time()

n = 2200000000000

def sum_divisors(n, divisor):
    sqrtN = n**0.5    
    i = divisor
    t = 0
    while i <= sqrtN:
        if not n%i:
            divisor = i
            if i == n//i or i == 1:
                #print(i, n//i)
                t+= i
                break
            t += sum([i,n//i])
            #print(i, n//i)
            break 
        i += 1
    if i < sqrtN:
        divisor += 1
        t += sum_divisors(n, divisor)
    return t

def amic(n):
    t =0
    for i in range(n):
        if sum_divisors(sum_divisors(i,1), 1)== i:
            print(i)
            t+=i
    print(t)
    


def primeFactorize(n):
    li = []
    p = []
    i = 1
    while n > 1:
        i += 1
        if not n%i:
            n = n//i
            p += [i]
            li += [n]
            #print(li)
            
            i -= 1
    print(p)
    return p

def prod(t):
    t = list(t)
    #print(t)
    p = 1
    for i in t:
        p *= i
    return p


abundant= [12]
ans = []
lab = 1
n= 37
for i in range(12,n):
    p = sum_divisors(i, 1)
    if p>i:
        abundant += [i]
        lab += 1
        for j in range(lab):
            if abundant[j] + i<n:
                #print(abundant[j], ans)
                ans += [abundant[j] + i]
            else:
                break


#print(lab, abundant)
ans = list(set(ans))
print((ans))

print(sum(list(range(28124))),sum(ans))
        

print('___________________%s__________________'%(time() - st))
st = time()
abundant = []
n= 5000
for i in range(12,n):
    p = sum_divisors(i, 1)
    if p>i:
        abundant += [i]

#print(len(abundant), abundant)
ab, na = [],0
l = len(abundant)
for n in range(1,n):
    for i in range(l):
        ai = abundant[i]
        if n > ai and n-ai >= ai:
            try:
                abundant.index(n-ai,i,n)
                #print("sum of two abundant", n, i)
                ab += [n]
                break
            except:
                ""
        #else:
            #print("not sum of 2 abundant", n, i)
            #na += n
            #break

print(len(ab))
#print(na)

print('___________________%s__________________'%(time() - st))