The 8-Queens Puzzle Solution for HP Calculator
My First Computer Program, the 8-Queens Puzzle
- It is actually the HP-28s that i did my first programing.
- I never took any programing course, but am a avid learner of math and computer stuff.
- I read the manuals from cover to cover.
- 〔see Emacs Idolization: Have You Read the Emacs Manual From Cover to Cover?〕
- My first program is a program to solve the 8 queens puzzle: position 8 queens on chess board so that none can check the other.
- It was a puzzle posted to CompuServe's math forum (CompuServe is a pre-internet online server in early 1990s).
- At the time, i'm also a crazy fan of recreational math.
- I took it upon myself to solve this by programing my super calculator to automatically generate the solution.
- The 8-queens puzzle is typically solved by a backtracking algorithm.
- This is what i did, even though at the time i know nothing about computer science or programing. 〔see my Computing Experience (Impression Of Lisp from Mathematica)〕
- Also, a couple years later i realized, that there is a bug in my program.
- That is, my program misses 8 solutions (or was it 4?).
HP RPL code for the 8-queens puzzle:
HP-28s calculator Eight Queens Puzzle solution program A1 1.1 P1 « A1 1 ->LIST 'S1' STO A1 P2 » P2 « 1 + P3 » P3 « IP .1 + P4.1 » P4.1 « 1 CF 5 CF -> a « 7 1 FOR N IF 5 FS? THEN ELSE IFERR S1 n GET THEN DROP2 ELSE -> b « IF a FP b FP not= THEN 1 SF ELSE 1 CF 5 SF END » END END -1 STEP a » P4.2 » P4.2 « -› a « 7 1 FOR n IF 5 FS? THEN ELSE IFERR S1 n GET THEN DROP2 ELSE -> b « 1 7 FOR m IF 5 FS? THEN ELSE IF a m 1.1 * b + not= a m .9 * b + not= AND THEN 1 SF ELSE 1 CF 5 SF END END NEXT » END END -1 STEP a » IF 1 FS? THEN P6 ELSE P5 END » P5 « 2 CF -> a « IF a a IP .8 + == THEN 2 CF ELSE 2 SF END a » IF 2 FS? THEN P8 ELSE P9 END » P6 « -> a « S1 LIST-> 1 + a SWAP ->LIST 'S1' STO a » P7 » P7 « 3 CF IF S1 SIZE 8 == THEN 3 SF ELSE 3 CF END IF 3 FS? THEN P11 ELSE P2 END » P8 « .1 + P4.1 » P9 « 4 CF -> a « IF a 1.8 == THEN 4 SF ELSE 4 CF END a » IF 4 FS? THEN P11 ELSE P10 END » P10 « IF S1 { 1.8 } = THEN P11 ELSE IF S1 SIZE 1 == THEN DROP S1 LIST-> DROP .1 + 1 ->LIST 'S1' STO S1 LIST-> DROP P2 ELSE DROP S1 S1 SIZE GET S1 LIST-> SWAP DROP 1 - ->LIST 'S1' STO END P5 » P11 « DROP S1 1 3 START 110 .3 BEEP 220 .3 BEEP 440 .3 BEEP 880 .3 BEEP NEXT »