Project Euler

Problem 23: Non-abundant sums

Find the sum of all the positive integers which cannot be written as the sum of two abundant numbers. Run this solution at repl.io here.

      
        def pdivisors(n):
          #calculate the proper divisors of 'n'
          div = [1]
          #Idea: all factors come in pairs
          #this algorithm is BIG O[sqrt(n)]
          for f in range(2,int(n**0.5)+1):
            if f*f == n:
              div.append(f)
            elif n % f == 0:
              div.append(f)
              div.append(n//f)
          return(div)

        abnums = set(i for i in range(1,28123) if sum(pdivisors(i)) > i)

        sum_abun = []
        for i in abnums:
          for j in abnums:
            if i + j < 28123:
              sum_abun.append(i+j)

        #remove duplicates in sum_abnums and 
        sum_abun = set(dict.fromkeys(sum_abun))

        total = 0

        candidates = [i for i in range(1,28123)]

        for n in sum_abun:
          candidates.remove(n)

        print(sum(candidates))
      
    

back to code menu