テンパズルは、4桁の数字を一桁の数字4つとみなし、これに四則演算を用いて10を作る遊びである。これをPythonで解いてみた。ZeroDivisionErrorが出ても例外処理で難なく処理できるので楽である。
import itertools def is_make10(target): nums = [x for x in itertools.permutations(target)] opers = set([y for z in itertools.combinations_with_replacement("+-*/",3) for y in itertools.permutations(z,3)]) models = ["({a}{x}{b}){y}({c}{z}{d})", "(({a}{x}{b}){y}{c}){z}{d}", "{a}{x}({b}{y}({c}{z}{d}))", "({a}{x}({b}{y}{c})){z}{d}", "{a}{x}(({b}{y}{c}){z}{d})"] for num in nums: for oper in opers: for model in models: try: tmp = model.format(a = num[0], b = num[1], c = num[2],d = num[3], x = oper[0],y = oper[1], z = oper[2]) if eval(tmp) == 10: return True except ZeroDivisionError: continue else: return False ans = ["".join(z) for z in itertools.combinations_with_replacement("0123456789",4) if is_make10(z) == False] print(ans)
以下にテンパズルができない数字リストを掲載する。
['0000', '0001', '0002', '0003', '0004', '0005', '0006', '0007', '0008', '0009', '0011', '0012', '0013', '0014', '0015', '0016', '0017', '0018', '0022', '0023', '0024', '0026', '0027', '0029', '0033', '0034', '0035', '0036', '0038', '0039', '0044', '0045', '0047', '0048', '0049', '0056', '0057', '0058', '0059', '0066', '0067', '0068', '0069', '0077', '0078', '0079', '0088', '0089', '0099', '0111', '0112', '0113', '0114', '0116', '0117', '0122', '0123', '0134', '0144', '0148', '0157', '0158', '0166', '0167', '0168', '0177', '0178', '0188', '0222', '0233', '0236', '0269', '0277', '0279', '0299', '0333', '0335', '0336', '0338', '0344', '0345', '0348', '0359', '0366', '0369', '0388', '0389', '0399', '0444', '0445', '0447', '0448', '0457', '0478', '0479', '0489', '0499', '0566', '0567', '0577', '0588', '0589', '0599', '0666', '0667', '0668', '0677', '0678', '0689', '0699', '0777', '0778', '0788', '0799', '0888', '1111', '1112', '1113', '1122', '1159', '1169', '1177', '1178', '1179', '1188', '1399', '1444', '1499', '1666', '1667', '1677', '1699', '1777', '2257', '3444', '3669', '3779', '3999', '4444', '4459', '4477', '4558', '4899', '4999', '5668', '5788', '5799', '5899', '6666', '6667', '6677', '6777', '6778', '6888', '6899', '6999', '7777', '7788', '7789', '7799', '7888', '7999', '8899']