// you’re reading...
1 Star2 Stars3 Stars4 Stars5 Stars (47 votes, average: 5.00 out of 5)
Loading...

Project Euler Solutions

Project Euler 23 Solution

Project Euler 23 Solution

Project Euler 23: Find the sum of all the positive integers which cannot be written as the sum of two abundant numbers.


Problem Description

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.

Analysis

According to Wolfram Mathworld’s discussion on Abundant Numbers, “Every number greater than 20161 can be expressed as a sum of two abundant numbers. ” So our upper bound is 20161 instead of 28123.

Using our routine from problem 21 to calculate the sum of proper divisors we create a set of abundant numbers on the fly and use Python’s set operations to make the necessary comparisons. We then add all the numbers that can’t be formed from the sum of two abundant numbers.

h_mark_sm-05bceb881aa02b72d688d21db01df5d8
This program and method
solves all test cases
on HackerRank

Project Euler 23 Solution

Runs < 0.530 seconds in Python 2.7.
from Euler import d    # d(n) returns the sum of proper divisors for n
 
L, s = 20161, 0
abn = set()

for n in range(1, L+1):
    if d(n) > n:
        abn.add(n)
    if not any( (n-a in abn) for a in abn ):
        s+= n
 
print "Project Euler 23 Solution =", s
download arrowUse this link to get the Project Euler 23 Solution Python 2.7 source.

Answer

Slowly swipe from either end beginning with the white vertical bar to get an idea of the starting or ending digits. For less drama, just double click the answer area. The distance between the two bars will give you an idea of the magnitude. Touch devices can tap and hold the center of the box between the two bars and choose define to reveal the answer.
|4179871|

Afterthoughts

Project Euler 23 Solution last updated

Discussion

10 Responses to “Project Euler 23 Solution”

  1. Hello, maybe 3 years too late, but I hope you can help me find the flaw in my code. I have checked that my versión of the function for summing up proper divisors works alright; so the problems must be the other one, yet I can’t figure out where.

    def divSum(n):
        div = 1
        divs = []
        while True:
            if n  n:
                abundants.add(n)
            for number in abundants:
                if (n - number) in abundants:
                    isSum = True
            if not isSum:
                totalSum += n
        return totalSum
    

    Posted by Miguel | November 27, 2016, 1:08 PM
  2. I get an error when I try to import from Euler, so I constructed the d list from scratch. This is my code and it gives an answer of 4123438 which is apparently wrong. Obviously I am a beginner in Python. Where have I gone wrong? Thank you.

    sm=0
    d = []

    for x in range(1,20163):
    sm=0
    for y in range(1,x-1):
    if x%y == 0:
    sm = sm + y
    next

    d.append(sm)

    # print x, sm
    next

    #for x in range(1,len(d)):
    # print x, d[x]

    L, s = 20162, 0
    abn = set()

    for n in range(1, L):
    if d[n] > n:
    abn.add(n)
    if not any( (n-a in abn) for a in abn ):
    s += n

    print “Answer to PE23 =”, s

    Posted by Thomas Babonis | March 13, 2014, 5:41 AM
    • Here’s a solution with the ‘d’ function explicitly defined.

      #Calculate the sum of proper divisors for n
      def d(n):
          s = 1
          t = n ** 0.5
          for i in range(2, int(t)+1):
              if n % i == 0: s += i + n/i
          if t == int(t): s -= t    #correct s if t is a perfect square
          return s
       
      L, s = 20162, 0
      abn = set()
       
      for n in range(1, L):
          if d(n) > n:
              abn.add(n)
          if not any( (n-a in abn) for a in abn ):
              s += n
       
      print "Answer to PE23 =", s
      

      You can find my Python Euler library here:
      Project Euler Library

      Posted by Mike | March 13, 2014, 5:03 PM
  3. Can you explain this line in your second function?

    if not any( (n-a in abn) for a in abn ):
    s += n

    and also this line in your first function:
    if t == int(t): s -= t

    Posted by John | February 2, 2013, 2:04 PM
  4. Hi, I’m new on python and I just start the project-euler. I think I learned a lot from your code. It’s really amazing!

    Posted by kilimanjaroup | August 17, 2012, 3:55 AM

Post a comment