1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
from itertools import combinations, permutations from collections import defaultdict from number_theory import is_prime # find combinations of three random numbers in play # after reveal <n> def find_in_play(sf3_to_h, n, snp): # index hidden numbers on their last <n> digit(s) sf_to_h, n = defaultdict(set), 10 ** n for sf3, hs in sf3_to_h.items(): sf_to_h[sf3 % n].update(hs) # for each last <n> digits find the random numbers that # stay in play (i.e. don't win or lose) for these digits sf_to_r = defaultdict(set) for sf, hs in sf_to_h.items(): for r in snp: # wins if not any(h % r for h in hs): continue # losses if all(h % r for h in hs): continue # still in play sf_to_r[sf].add(r) return sf_to_r # non-primes less than 30, also eliminating those that # always win (1, 9) and never win (10, 20) dg = set(range(2, 30)).difference([9, 10, 20]) snp = set(x for x in dg if not is_prime(x)) # index the hidden numbers on their three digit suffix sf3_to_nbrs = defaultdict(set) for p in permutations('123456789'): h = int(''.join(p)) sf3_to_nbrs[h % 1000].add(h) # find sets of random numbers in play after the second reveal # for each two digit suffix sf_to_r = find_in_play(sf3_to_nbrs, 2, snp) # after the third reveal we are only interested in three random # numbers, two of which win and one of which loses; consider # sets of random numbers in play for each two digit suffix for sf, rs in sf_to_r.items(): # ... and each combination of three of them for r3 in combinations(rs, 3): # consider each value of the thousands digit of the hidden numbers for d3 in range(100 + sf, 1000 + sf, 100): # look for a combination of three random numbers and a three # digit suffix that results in two wins and one loss wins = [all(h % r == 0 for h in sf3_to_nbrs[d3]) for r in r3] if wins.count(True) == 2: losses = [all(h % r != 0 for h in sf3_to_nbrs[d3]) for r in r3] if losses.count(True) == 1: fs = 'The three digit suffix is {} with random numbers {}.' print(fs.format(d3, r3)) |