Re: Programming Probability of puzzle in Mathematica
- To: mathgroup at smc.vnet.net
- Subject: [mg44912] Re: Programming Probability of puzzle in Mathematica
- From: drbob at bigfoot.com (Bobby R. Treat)
- Date: Mon, 8 Dec 2003 02:29:19 -0500 (EST)
- References: <bqv1op$t07$1@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
I found 19 positive solutions, only of which involve all nine digits.
One of those (take your pick) is the numerator for the probability you
want. If every draw is a matrix with all nine digits in it, the
denominator is 9 factorial. If every draw is a matrix of 9 one-digit
numbers (with possible repeats), the denominator is 9^9.
Here's a brute force way to find the solutions. (It's probably
unnecessarily complicated, but maybe it's instructive.)
We'll need KSubsets, so load...
<< DiscreteMath`Combinatorica`
Here are the equations and variables.
row[j_] := Sum[a[i + 3(j - 1)], {i, 1, 3}] == 15
column[j_] := Sum[a[j + 3(i - 1)], {i, 1, 3}] == 15
diagonal[1] := a[1] + a[5] + a[9] == 15
diagonal[2] := a[3] + a[5] + a[7] == 15
equations = Flatten@{row /@ Range@3, column /@ Range@3, diagonal /@
Range@2}
variables = Array[a, 9]
Solutions look like this (neglecting that solutions have to be
integers, etc.):
Off[Solve::"svars"]
Solve[equations, variables]
We'll solve for the last 7 variables in terms of the first two:
rules = First@Solve[equations, Drop[variables, -2]]
Given an eighth and ninth digit, the following function computes the
others and checks whether the solution is positive:
positive[{x_, y_}] := Module[{s, ok},
s = variables /. rules /. {a[8] -> x, a[9] -> y};
ok = And @@ Thread[s >= 1];
{s, ok}
]
Here's a list of the positive solutions:
vectors = Cases[positive /@ KSubsets[Range@9, 2], {x_List, True} :>
x];
vectors // Length
Next we display them as 3x3 matrices:
matrix[x_List] /; Length[x] == 9 :=
Table[x[[i + 3(j - 1)]], {i, 1, 3}, {j, 1, 3}]
matrix /@ vectors // TableForm
If we want solutions involving all 9 digits, we replace the 'positive'
function as follows:
nine[{x_, y_}] := Module[{s, ok},
s = variables /. rules /. {a[8] -> x, a[9] -> y};
ok = Union[s] == Range[9];
{s, ok}
]
vectors = Cases[nine /@ KSubsets[Range@9, 2], {x_List, True} :> x];
vectors // Length
matrix /@ vectors // TableForm
Bobby
mathtutoring at comcast.net (art burke) wrote in message news:<bqv1op$t07$1 at smc.vnet.net>...
> Hi to all:
>
> Not being a programmer, but yet inquisitive, I'd like to come up with
> several small programs that compute the probability of random numbers
> being
> inserted into let's say, a magic cube, triangle, or such that all
> rows, columns, and diagonals add up to a certain number.
>
> Let's say a 3x3 magic square, using the numbers 1,2,3,4,5,6,7,8 and 9.
> Of course, if you know how to put them into the magic square, all
> rows, diagonals, and columns add up to 15.
>
> Having Mathematica insert random numbers in cells, compute all sums
> and see if it has it correct, and keep up the repetions until it comes
> up with an average probability, let's say after 1000 tries....What
> would be the probability?
>
> I've been quit interested in this for a while, but am wondering how it
> would be done??
>
> Thanks for all the help,
>
> Art