Monthly Archives: May 2011

Euler Problem 97

I saw that many people had solved this one. Must be easy. It was. I suppose the challenge would be to get it down to as fast as possible. So I employed a simple power of 2 bit-hack and go it down to 14 minutes, with the core-code down to 9 lines. I suppose I could do much better given time & energy, but hey, it works fine.

#Euler97
class Problem:
    def Solution(self):
        a = 1 << 100000
        M = 1
        for i in range (0,78):
            M *= a
        M = (M * (1 << 30457) * 28433) + 1
        Mersenne = str(M)
        size = len(Mersenne)
        for i in range(size-10,size):
            print Mersenne[i],
if __name__ == '__main__':
    P = Problem()
    P.Solution()

Time to Jump Around

I looked at the next few Euler problems and realized they were all related. I suddenly got very bored. In an effort to keep myself from starting to play with yet another programming language, I thought it best to start jumping around. Got to reach at least 100 solved.

Euler Problem 81

I haven’t really been updating lately, even though I have been working on a few problems.
This one is very similar to the triangle problems of 18 & 67.

#Euler81
import sys
class Problem81:
    def __init__(self):
        pass
    def Solution(self):
        M = []
        f = open("matrix.txt", "r")
        for row in f.readlines():
            newRow = []
            for column in row.split(","):
                newRow.append(int(column))
            M.append(newRow)
        f.close()
        for x in range (len(M)-1,-1,-1):
            for y in range (len(M)-1,-1,-1):
                if x == len(M)-1 and y == len(M)-1:
                    continue
                if x < len(M)-1:
                    s1 = M[x+1][y]
                else:
                    s1 = sys.maxint
                if y < len(M)-1:
                    s2 = M[x][y+1]
                else:
                    s2 = sys.maxint
                if s1 < s2:
                    M[x][y] += s1
                else:
                    M[x][y] += s2
        return "Answer = " + str(M[0][0])
if __name__ == '__main__':
    P = Problem81()
    print P.Solution()