it an answer for Euler project #4.
A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 99.
Find the largest palindrome made from the product of two 3-digit numbers.
Answer:
906609
code is:
from multiprocessing import Pool
from itertools import product
def sym(lst):
rst=[]
for x,y in lst:
tmp=x*y
if rec(tmp):
rst.append(tmp)
return rst
def rec(num):
num=str(num)
if num == "".join(reversed(num)): return True
else: return False
if __name__ == "__main__":
pool=Pool(processes=8)
lst=product(xrange(100,1000),repeat=2)
rst=pool.map(sym,lst)
#rst=sym(lst)
print max(rst)
when I run this:
# TypeError:'int' object is not iterable
but I can’t understand it…isn’t list iterable? or is there an error in my code?
Your problem is with the function
sym.symis being passed the first element of your product iterable. (e.g.lst = (100,100)). When you get to theforloop, you’re iterating overlstand then trying to unpack it into two numbers — equivalent to:Which fails for obvious reasons.
I think you probably want to get rid of the for loop all-together — which was probably an artifact of your serial version.
The traceback was somewhat cryptic — Apparently the traceback gets returned to the
Poolwhich then gets re-raised … But the way it is done makes it a little difficult to track.Sometimes, when debugging these things it is helpful to replace
Pool.map()with the regular version ofmap. Then, any exceptions which get raised are raised on your main “thread” and the tracebacks can be a little easier to follow.