Greg Christian's weblog

April 29, 2011

Project Euler – Problem 30 – Solved with Python

Filed under: Project Euler: 26, 28, 29, 30, 32, 33,Python — Greg Christian @ 3:15 pm
Tags: , ,

Problem:

Surprisingly there are only three numbers that can be written as the sum of fourth powers of their digits:

  • 1634 = 1**4 + 6**4 + 3**4 + 4**4
  • 8208 = 8**4 + 2**4 + 0**4 + 8**4
  • 9474 = 9**4 + 4**4 + 7**4 + 4**4

As 1 = 1**4 is not a sum it is not included.

The sum of these numbers is 1634 + 8208 + 9474 = 19316.

Find the sum of all the numbers that can be written as the sum of fifth powers of their digits.

One Possible Solution:

def split_number(x):
    """Split number x into a list yy of individual numbers"""
    yy = list(str(x))
    return yy

def sum_of_powers(x, y, length_y):
    """Find the sum of the individual numbers raised to the 5th power
    and determine if they are equal to x"""
    x1, x2, x3, x4, x5, x6 = 0, 0, 0, 0, 0, 0
    if length_y == 1:
        x1 = int(y[0]) ** 5
        if x1 == x:
            return "True"
        else:
            return "False"
    elif length_y == 2:
        x1 = int(y[0]) ** 5
        x2 = int(y[1]) ** 5
        if x1 + x2 == x:
            return "True"
        else:
            return "False"
    elif length_y == 3:
        x1 = int(y[0]) ** 5
        x2 = int(y[1]) ** 5
        x3 = int(y[2]) ** 5
        if x1 + x2 + x3 == x:
            return "True"
        else:
            return "False"
    elif length_y == 4:
        x1 = int(y[0]) ** 5
        x2 = int(y[1]) ** 5
        x3 = int(y[2]) ** 5
        x4 = int(y[3]) ** 5
        if x1 + x2 + x3 + x4 == x:
            return "True"
        else:
            return "False"
    elif length_y == 5:
        x1 = int(y[0]) ** 5
        x2 = int(y[1]) ** 5
        x3 = int(y[2]) ** 5
        x4 = int(y[3]) ** 5
        x5 = int(y[4]) ** 5
        if x1 + x2 + x3 + x4 + x5 == x:
            return "True"
        else:
            return "False"
    elif length_y == 6:
        x1 = int(y[0]) ** 5
        x2 = int(y[1]) ** 5
        x3 = int(y[2]) ** 5
        x4 = int(y[3]) ** 5
        x5 = int(y[4]) ** 5
        x6 = int(y[5]) ** 5
        if x1 + x2 + x3 + x4 + x5 + x6 == x:
            return "True"
        else:
            return "False"
    else:
        print "Error Message"
        
def main():
    """Main Program"""
    L = []
    for x in range(2, 1000000):
        y = split_number(x)
        length_y = len(y)
        xy = sum_of_powers(x, y, length_y)
        if xy == 'False':
            pass
        else:
            L.append(x)
    print "L = ", L
    print "Sum of list L = ", sum(L)

if __name__ == '__main__':
    main()

Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Theme: Rubric. Blog at WordPress.com.

Follow

Get every new post delivered to your Inbox.