Several people have already written programs based on the
Numbers Game from the TV program
Countdown, but anyway here is another one, written in Python.
As has been noted elsewhere, a set of 2 large + 4 small or 1 large + 5 small typically covers a large proportion of target numbers (101 to 999), and there are typically many ways of expressing each target using some or all of the 6 numbers.
These sets each cover all targets from 101 to 999. The lists of solutions (
) contain all targets from 0 until a gap above 999 is reached:
5 6 7 8 9 75 (five consecutive numbers)
2 5 7 8 9 10 (six small numbers)
4 7 7 10 25 100 (a repeated number)
6 7 7 9 9 100 is perhaps more noteworthy, covering every target number except the number 367
8 9 25 50 75 100 is the best set containing all four large numbers, covering 885 targets
The code constructs solutions for a few games that have appeared on the TV show:
- 3 6 25 50 75 100 with a target of 952 , the James Martin 952 game, which has 2 solutions, (3×75×(6+100)-50)÷25 which was found by the contestant, and 25+(6×75×(3+100))÷50.
- 1 4 8 9 10 50 with a target of 500 ,possibly the easiset game ever? for which my program finds 21 distinct solutions .
- Some games solved by George Ford , one of the best countdown contestants.
Most of the complexity of the program is to do with minimising brackets,
e.g.
(((((6)+(7))-(9))×(7))×(9))-(100)=(6+7-9)×7×9-100
and avoiding solutions that are essentially the same, e.g.
152=(6+7-9)×7×9-100=7×(7-9+6)×9-100.
The program also excludes "frivolous" solutions that contain an expression that equates to zero, e.g. 500 = (9-8-1)×4+50×100, solutions that involve multiplying or dividing by 1, e.g. 500 = (9-8)×50×100 or 500 = 50×100÷1 and solutions that have other redundancies, e.g. 500 = 9+10×50-1-8.
# -*- coding: cp1252 -*-
from itertools import combinations
def wrap(t, condition=True):
return ''.join(["(",t,")"]) if condition else t
def expressions( A, processed=set() ):
""" initially, A is an array of numbers
In recursive calls, A is an array of tuples (n,t,o,x,y,h) where
n is a number,
t is a text expression of n,
o is the last operation applied to x,y to produce n = xoy
h is the history of the expression, containing all the numbers produced in the construction
of the expression
"""
if type(A[0])==int: # inital call, so convert to array of tuples
A = [ (A[i],str(A[i]),"",0,0, frozenset((A[i],)) ) for i in xrange(len(A))]
processed=set()
else: # recursive call, so return the expression formed in the parent
n,t,o,x,y,h = A[0]
if (n,t) not in processed:
yield A[0]
processed.add( (n,t) )