Project Euler – Problem 30 – Solved with Python

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()
Advertisements

Project Euler – Problem 29 – Solved with Python

Problem:

Consider all integer combinations of a**b for 2 <= a <= 5 and 2 <= b <= 5:

  • 2**2=4, 2**3=8, 2**4=16, 2**5=32
  • 3**2=9, 3**3=27, 3**4=81, 3**5=243
  • 4**2=16, 4**3=64, 4**4=256, 4**5=1024
  • 5**2=25, 5**3=125, 5**4=625, 5**5=3125

If they are then placed in numerical order, with any repeats removed, we get the following sequence of 15 distinct terms:

4, 8, 9, 16, 25, 27, 32, 64, 81, 125, 243, 256, 625, 1024, 3125

How many distinct terms are in the sequence generated by a**b for 2 <= a <= 100 and 2 <= b <= 100?

**********

Using a list L for each term, we can then find the set of that list which will eliminate duplicates. Then find the length of that set with len

One Possible Soultion:

def get_terms():
    """Find the terms and put them in a list L,
    return the set of list L"""
    L = []
    for a in range(2, 101):
        for b in range(2, 101):
            term = (a ** b)
            L.append(term)
    return set(L)

def main():
    """Main program"""
    x = get_terms()
    print "Number of Terms = ", len(x)
    
if __name__ == '__main__':
    main()