1 2 3 4 5 6 7 8 9 10 11 |
from itertools import permutations for b, c in permutations((1, 2, 3, 4), 2): matches = [h for h in range(53 * 18) if not ( (b + h // 22 - h // 21) % 5 or (c + h // 23 - h // 21) % 5)] if len(matches) == 1: w, h = divmod(matches[0], 18) print(f'Hole {h + 1} in Week {w + 1}.') |

set h = 18*week + hole.,

The balls coincide when

(a + (h – h mod 21)/21) mod 5 = (b + (h – h mod 22)/22) mod 5 =(c + (h – h mod23)/23) mod 5

Then h can be converted back to a hole in the range 1 to 18

I didn’t spot that one of the balls could be fixed until I saw Brian’s solution, but using that observation:

1 2 3 4 5 6 7 8 9 10 |
from itertools import permutations, product a=0 for b,c in permutations(range(1,5),2): matches = [h for h in xrange(53*18) if (a + (h - h%21)/21)%5 == (b + (h - h%22)/22)%5 ==(c + (h - h%23)/23)%5] if len(matches)==1: h = matches[0] print "hole ", h%18+1, "week = ", h/18+1 |

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 |
from itertools import permutations # Use zero based values to make the maths simpler: 0..4 for (a, b, c), # 0..52 for week numbers and 0..17 for hole numbers; zero is used for # a's initial value since only differences between a, b and c matter for bc in permutations((1, 2, 3, 4), 2): # for registering holes where a, b and c are equal found = False # for 53 weeks, each of 18 holes, the zero based hole number (h) # within a year is in the range 0..953 for h in range(954): # increment (mod 5) each of (a, b, c) after each 21, 22 and 23 # holes respectively a, b, c = ((x + h // y) % 5 for x, y in zip((0,) + bc, (21, 22, 23))) # record the global hole number when a, b and c are equal (we don't # continue with a, b, and c combinations giving multiple solutions) if a == b == c: if found: break found, h_no = True, h else: # there is only one occasion where a, b and c are equal (use one # based numbers in the output) if found: week, hole = divmod(h_no, 18) print(f'Hole {hole + 1} in Week {week + 1}.') |

Erling Torkildsen has developed an interactive GeoGebra model for this teaser here.

]]>