The magazine of the Melbourne PC User Group

Solving puzzles with BASIC
Ken Holmes
 

The program SQPUZZLE.BAS is available here


Tired of surfing and sorting the information from the disinformation and the trivia on the web? Why not try some programming for a change of pace to give yourself a different challenge and a another type of satisfaction?

After you get a feel for the rudiments of a programming language, the best way to learn is to write small programs. Numerical puzzles are excellent since they force you to persist with such things as "for loops", "while loops" and "if statements" until they work. They pop up in newspapers and magazines and are usually meant to be solved by a combination of logic and trial-and-error using Peter Smith's "Necktop" computer. It is rewarding to use this approach but, after doing so, try writing a program to do it. You may also contrive your own puzzles and need not be constrained to keep the number of possibilities to a reasonable task for the brain. Assuming you can write the program, the computer will not be fazed by sifting through a million possible solutions; you might spend hours getting it to work but it usually spits out the answers in a fraction of a second. Oh well! On to the next puzzle.

Let us pose a question: "How many pairs of four-digit squares are there that use 8 different digits?" While we are about it: "How many pairs can we combine with a two-digit square to use all ten of the digits 0 - 9?"

Plan of action

We will use QBasic, available from later DOS versions, as BASIC is probably the most familiar language. Type in the program and see if you can get it to work. The four-digit squares range from 1024 (32^2 ) to 9801 (99^2 ); there are 68 of them and the total number of possible pairs is 68*67/2=2278. Some of the squares have repeated digits in them so we might as well eliminate those; we will find that there are 36 that don't repeat digits, which reduces our task to 36*35/2=630 possible pairs.

We will use a two-dimensional array, sq4(36, 4), having rows 0 to 36 and columns 0 to 4; by comparing digits in each square, we will fill rows 1 to 36 with the useful ones, with the square in column 0 and the individual digits in columns 1 - 4. The
     
FOR n/NEXT n
loop uses some modular arithmetic to separate the digits and two nested loops to compare all digits. If the same digit occurs twice, the loops are exited with the loop variables retaining their values.

If the loops are not interrupted, the variables acquire values 1 above their upper limits; then we know we have correctly filled the row and can move to the next by incrementing the count.

At this stage, we will also fill the array, sq2(6, 2) with the six two-digit squares, 16 to 81 (and their digits); none use the same digit twice.

So much for the preliminaries. Now we will use two nested loops to examine all possible pairs. Inside we will compare each digit in the first member with each digit in the second. If we aren't thrown out, we have found a pair meeting our first puzzle and can print them (ex column 0); now we can use three nested loops to compare digits with each of our two-digit squares.

Lo and behold - we find there are two sets that use all the digits. Not likely to change the course of human history - but then! To satisfy your curiosity, the two solutions are
  • 2304, 7569 and 81
  • 3249, 7056 and 81.
I t is doubtful that the common 81 has some deep and meaningful significance.

When you get this program to work, your mission, should you choose
to accept it, is: "How many sets, of two three-digit squares and one four-digit square, use all the digits?" Prizes will be available to the first to ring me with the solution(s); these will be my hearty congratulations and your own deep inner satisfaction. What the heck! I'll extend the prizes to any who solve it - or even try.

If you are still interested, check pairs of five-digit squares (you will need to use long integers).

A different type of puzzle you might try is the one in the Saturday Age; this is a five-by-five array of digits where you are given the row and column totals and you must find the digits. You can write a brutal program with ten nested loops and lots of test calculations, but with a bit of logic you can reduce to three or four loops and much less work for the computer - this is more elegant and satisfying.

Reprinted from the August 1998 issue of PC Update, the magazine of Melbourne PC User Group, Australia

[About Melbourne PC User Group]