Showing posts with label problem 14. Show all posts
Showing posts with label problem 14. Show all posts

2017-06-16

Project Euler Problem 14



I first ran a code that went through all the 1M starting values, and when it was finished, it printed the chain length instead of what starting number the chain had -.-. When I modified the code to solve the problem I made it stop at that chain length instead of continuing searching.

chain = []
newChain = []
val = 0

def odd_val(val):
    odd = 3 * val + 1    return int(odd)


def even_val(val):
    even = val / 2    return int(even)


for value in range(10 ** 6, 1, -1):
    print(value, len(chain))
    while value != 1:
        if value % 2 == 0:
            value = even_val(value)
            newChain.append(value)
        else:
            value = odd_val(value)
            newChain.append(value)
    if len(newChain) > len(chain):
        chain = newChain
        print("New chain!")
    if len(chain) == 524:
        break    newChain = []

print(len(chain), val)

Hmmm now that I look through the print out there seems to be something weird in the code. I'm going to make it look nicer before heading on to the next problem.

I ran out of energy for fixing it. Hopefully I'll turn back sometime in the future.


/Ludvig