Saturday, March 14, 2009

Project Euler 43

While working on euler functions, i have managed to come up with quite a library of functions that I use to solve problems.

Today I was playing around with Python 3.0 to see how many of the libraries can be converted, refactored or removed while utilizing the new features of python. First one that can potentially go is the permutations function since itertools now has one.

So here is my ( brute force ) solution to 43 using the permutations from itertools . Takes around 15 seconds to execute

import time
from itertools import permutations

def checkForProperty(t):

if (int(t[1:4])%2) != 0:
return False

if(int(t[2:5])%3) != 0:
return False

if (int(t[3:6])%5) != 0:
return False

if(int(t[4:7])%7) != 0:
return False

if(int(t[5:8])%11) != 0:
return False

if(int(t[6:9])%13) != 0:
return False

if(int(t[7:10])%17) != 0:
return False

return True

r = time.time()

print (sum([int(''.join(x)) for x in permutations('1234567890') if x[0] != '0' and checkForProperty(''.join(x))]))

print (time.time() - r)

No comments: