def s(n,p=__import__("itertools").permutations): l=["_"]*21 for e in[[(a,x+10),(b,y+10),(c,10-z)]for a,b,c in p(n,3)for x,y in p(range(1,11),2)for z in range(1,11)if a*x+b*y==c*z][0]:l[e[1]]=str(e[0]) return`l`[2::5]+"\n"+" "*10+"^" ip = [ (1,1,1), (1,2,1), (3,1,5), (4,7,2) ] for e in ip: print e print s(e)