Rcjp's Weblog

September 16, 2001

Countdown numbers game

Filed under: physics — rcjp @ 11:57 am

No this isn’t physics, but I suppose it charitably comes under the heading of numerical methods ;) …

I wrote the following (probably badly written) program after watching Carol Vorderman struggle to find solutions for the numbers game on the Countdown TV program.

I can’t remember why I wrote it in Fortran 95 ?!? and I didn’t test it much, just on a couple of sets of numbers I don’t think she (or I!) could solve by hand in the 30 secs the contestants are given.

The problem is – given a set of 6 random integers try to arrive at a randomly chosen number just using +,-,*,/ (although, thinking about it, I’m not sure integer division is allowed)

For the second test case (try to use 50, 9, 9, 4, 7 and 10 to make 971) the program printed


    ***SOLUTION***
      9  *   4  =  36
      7  *  10  =  70
     50  -  36  =  14
     70  *  14  = 980
    980  -   9  = 971

    ***SOLUTION***
      9  *   7  =  63
     50  +  63  = 113
    113  -   4  = 109
      9  * 109  = 981
    981  -  10  = 971

    at least            2 solutions

and the code COUNTDOWN.F90 is…


    module solutions
      integer, parameter::ncards=6
      integer::nsols
      integer::sol(1000,ncards*2)
    end module solutions

    program countdown
      use solutions
      integer::numbers(ncards)
      integer::solunum(ncards)
      integer::solunum1(ncards)
      integer::ntarget
      character::soluop(ncards)

      numbers=(/50, 7, 5, 1, 8, 3/)
      ntarget=684

      numbers=(/50, 9, 9, 4, 7, 10/)
      ntarget=971
      solunum=0
      solunum1=0
      soluop=' '
      nsols=1
      call pair(numbers, ncards, solunum, solunum1, soluop, ncards, ntarget)
      print *, 'at least ',nsols-1, ' solutions'
    end program countdown

    integer function combine(n, m, op)
      character::op
      integer::n, m

      select case (op)
      case ('*')
         combine=n*m
      case ('+')
         combine=n+m
      case ('-')
         combine=n-m
      case ('/')
         combine=n/m
         if (mod (n,m) /= 0) combine=0
      case default
         combine=0
      end select
      if (combinesolsize*2) then
            print *, '***SOLUTION***'
            do m=solsize, 2, -1
               n1=solunum(m)
               n2=solunum1(m)
               select case (soluop(m))
               case ('*')
                  n3=n1*n2
               case ('+')
                  n3=n1+n2
               case ('-')
                  n3=n1-n2
               case ('/')
                  n3=n1/n2
               end select
               print '(i4, 2x, a, i4, 2x, a, i4)', n1,soluop(m), n2, '=', n3
            enddo
            sol(nsols,:)=nums
            nsols=nsols+1
            return
         endif
      enddo
    end subroutine printsolution
Advertisements

Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Create a free website or blog at WordPress.com.

%d bloggers like this: