def isVampire(a, b): c = a * b a = str(a) b = str(b) return not (a.endswith('0') and b.endswith('0')) and \ sorted(list(str(c))) == sorted(list(a + b)) def main(): fp = open('c:\\vamp.txt', 'w') for n in range(6, 10): for a in range(10 ** n, 10 ** (n+1)): for b in range(10 ** n, a+1): if isVampire(a, b): print('%s\t%s\t%s' % (a, b, a * b)) fp.write('%s\t%s\t%s\n' % (a, b, a * b)) fp.close() main()