MathGroup Archive 2003

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Programming Probability of puzzle in Mathematica

  • To: mathgroup at smc.vnet.net
  • Subject: [mg44906] Re: Programming Probability of puzzle in Mathematica
  • From: bobhanlon at aol.com (Bob Hanlon)
  • Date: Mon, 8 Dec 2003 02:29:15 -0500 (EST)
  • References: <bqv1op$t07$1@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

Needs["DiscreteMath`Combinatorica`"];

Test function used to determine if matrix is a magic square:

Clear[test];

test[dim_Integer?Positive] := 
    test[dim] =  Module[
        {n=dim^2, mn,  m, mat, matT, t},
        mn=Tr[Range[n]]/dim;
        mat=Array[m, {dim,dim}];
        matT=Transpose[mat];
        t=And @@ Join[
              (Tr[#]==mn)& /@ mat,
              {Tr[mat]==mn},
              (Tr[#]==mn)& /@ matT,
              {Tr[Reverse/@mat]==mn}];
        Off[Part::pspec, Part::partw, Part::partd];
        Function[Evaluate[t /.m[i_,j_]->#[[i,j]]]]];

Testing a known False result

test[3][Partition[Range[9],3]]

False

Testing a known True result

test[3][{{6,7,2},{1,5,9},{8,3,4}}]

True

There is a very low probability of generating a magic square so you should use
many more trials than 1000

dim=3;s=Range[dim^2];

tries=200000; n=0;
Table[If[test[dim][m=Partition[RandomPermutation[s],dim]],Print[m];
      n=n+1],{tries}];
N[n/tries]


Bob Hanlon

In article <bqv1op$t07$1 at smc.vnet.net>, mathtutoring at comcast.net (art burke)
wrote:

<< 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?
 >><BR><BR>


  • Prev by Date: Re: Programming Probability of puzzle in Mathematica
  • Next by Date: Re: Programming Probability of puzzle in Mathematica
  • Previous by thread: Re: Programming Probability of puzzle in Mathematica
  • Next by thread: Re: Programming Probability of puzzle in Mathematica