# PUZZLES MATLAB Programs Useful for Puzzles

PUZZLES is a directory of MATLAB programs which were used to solve puzzles.

### Licensing:

The computer code and data files described and made available on this web page are distributed under the GNU LGPL license.

### Languages:

PUZZLES is available in a FORTRAN90 version and a MATLAB version.

### Related Programs:

ANAGRAM, a C++ program which determines anagrams of a string, by James Cherry;

LIFE_OPENGL, a C program which uses OpenGL to display the evolution of John Conway's "Game of Life", by Simon Green.

LIGHTS_OUT_OPENGL, a C++ program which sets up a "Lights Out" game and allows the user to solve it, using the OpenGL graphics window.

SUBANAGRAM, a FORTRAN90 program which finds words which are anagrams formed from some of the letters of a given master word.

SUDOKU, a MATLAB library which manipulates and solves Sudoku problems.

WORDSNAKE, a FORTRAN90 program which rearranges a list of words so that they have maximum overlap;

### CARD GAME PUZZLE

You are given a deck of CARD_NUM cards. (We'll assume CARD_NUM is 100.)

Your goal is to select the high card. We can assume the cards are a permutation of the integers from 1 to CARD_NUM.

However, your choice is made under the following rules: You may turn over one card at a time. When a card is turned over, you may declare that to be your choice, or else turn over another card. If you have not chosen a card by the end, then your choice is the final card.

If you have no idea what to do, and simply decide in advance to pick a card "at random", that is, for example, you decide to pick the 15th card before having seen any cards, then your probability of winning is 1/CARD_NUM.

The question is, can you do better than that?

Your strategy is as follows: always look at the first SKIP_NUM cards without choosing them. Then choose the very next card you encounter that is larger than the cards you skipped.

Using this program, you can easily see that skipping 5 cards is much better than picking one at random, skipping 10 is even better, and so on. Of course, you can't skip too many cards, and in fact, the results seem to be best for somewhere around 30 to 35 cards skipped. For problems like this, the optimal value is somewhere around 1 / e, where E is the base of the natural logarithm system.

• card_game.m, the source code;
• card_game.png, a plot of the probability estimates, using 1,000 trials for each value of SKIP_NUM.

### DIGIT PUZZLE

The seven digit puzzle The Digit Puzzle. It wasn't hard to find the seven unique digits of an integer such that the integer was divisible by each digit. But it was actually hard to figure out the order of the digits in the integer! Some thought was able to whittle down the last few digits to a small collection, and we knew the integer had to be a multiple of the least common multiple of the digits, but no way was obvious to actually determine the integer. At that point, Jeff Borggaard wrote the following MATLAB program to search for the answer.

### PERMUTATION PUZZLE

Let P1 and P2 be arbitrary permutations of the integers from 1 to 10.

Pair corresponding entries of the permutations to create the set of 10 points (P1(1),P2(1)) through (P1(10),P2(10)). Now subtract 5, that is, N/2, from the X and Y coordinates of each point so that their average value is zero. Then apply a rotation of 45 degrees to each vector.

Now instead of looking at 10 vectors (X(I),Y(I)), each of length 2, consider the data as two vectors X and Y, each of length 10. If the instructions have been followed correctly, then X are Y are perpendicular to each other in 10-dimensional space.

MATLAB code to verify a random example of this property is:

```         n = 10;
p1 = randperm ( n );
p2 = randperm ( n );
xy = [ p1; p2 ];
xy = xy - ( n / 2 );
angle = pi / 4;
A = [ cos ( angle ), - sin ( angle ); ...
sin ( angle ),   cos ( angle ) ];
xy = A * xy;
dot = xy(1,1:n) * xy(2,1:n)';
fprintf ( 1, '  The dot product is %f\n', dot );
```

Claim: This property is true for any pair of permutations P1 and P2.

Claim: This property is true even if P1 and P2 are equal.

Claim: This property is true for any positive integer N.

Claim: This property is not true for arbitrary values of the rotation angle. For instance, using an angle of pi/3 will not work.

Claim: Let Q be an arbitary vector of length N. Instead of P1 and P2, consider the vectors Q1=Q(P1) and Q2=Q(P2). The property is still true!

Puzzle: Can you verify that each of the claims is true? Can you explain why?

You can go up one level to the MATLAB source codes.

Last revised on 12 September 2009.