The Museum of HP Calculators

HP Forum Archive 04

[ Return to Index | Top of Index ]

Re: Some data on where HP users are
Message #1 Posted by Viktor Toth on 13 Sept 2000, 8:51 a.m.

Well, shame on me indeed. So perhaps I can redeem myself with this HP-42S program that computes the magic number. Admittedly, this program could be optimized (for instance, I bet it could be converted to use only the stack for computations instead of named variables) but hey, it does the job; on an HP-42S in "fast mode", it finds 851 in a little bit more than half an hour :-)

Enjoy!

Viktor

00 { 283-Byte Prgm }
01 LBL "PERC"
02 1
03 STO "X"
04 LBL 03
05 0
06 STO "S"
07 RCL "X"
08 .5546
09 XEQ 01
10 STO+ "S"
11 .1022
12 XEQ 01
13 STO+ "S"
14 .04
15 XEQ 01
16 STO+ "S"
17 .0353
18 XEQ 01
19 STO+ "S"
20 .0317
21 XEQ 01
22 STO+ "S"
23 .0235
24 XEQ 01
25 STO+ "S"
26 .0212
27 XEQ 01
28 STO+ "S"
29 .0165
30 XEQ 01
31 STO+ "S"
32 .0141
33 XEQ 01
34 STO+ "S"
35 .0129
36 XEQ 01
37 STO+ "S"
38 .0082
39 XEQ 01
40 STO+ "S"
41 .0059
42 XEQ 01
43 STO+ "S"
44 .0047
45 XEQ 01
46 STO+ "S"
47 .0035
48 XEQ 01
49 STO+ "S"
50 .0024
51 XEQ 01
52 STO+ "S"
53 .0012
54 XEQ 01
55 RCL+ "S"
56 x=0?
57 GTO 02
58 1
59 STO+ "X"
60 GTO 03
61 LBL 02
62 RCL "X"
63 BEEP
64 RTN
65 LBL 01
66 RCLx "X"
67 LASTx
68 x<>y
69 .5
70 +
71 IP
72 10000
73 X
74 RCL/ "X"
75 .5
76 +
77 IP
78 x-y
79 10000
80 X
81 -
82 x^2
83 RTN
84 .END.
      
Re: Some data on where HP users are
Message #2 Posted by Dave Hicks on 13 Sept 2000, 10:55 p.m.,
in response to message #1 by Viktor Toth

Ah! An answer with RPN code - I think this is the best so far.

      
This may be way too much information
Message #3 Posted by Steve (Australia) on 17 Sept 2000, 11:30 a.m.,
in response to message #1 by Viktor Toth

OK, I read the RPN solution just before I went to bed. I then decided to see if I could do better. So I sat in bed with a 41CV and came up with this.

It finds the answer (or an answer) in under 5 minutes on a bog standard 41, and the code is far from optimal.

First there;s the setup program:

01 LBL "SETUP"
02 .12
03 STO 10     // Reg 10 starts the block of proportions
04 .24
05 STO 11
06 .35
07 STO 12
08 .47
09 STO 13
10 .59
11 STO 14
12 .82
13 STO 15
14 1.29
15 STO 16
16 1.41
17 STO 17
18 1.65
19 STO 18
20 2.12
21 STO 19
22 2.35
23 STO 20
24 3.17
25 STO 21
26 4
27 STO 22
28 10.22
29 STO 23
30 55.46
31 STO 24

32 LBL "S2" // OK, so I needed to test some things a few times :-) 33 10.024 // ISG value for data block 34 STO 02 // Reg 2 stores an initial ISG value for the data 35 RCL 10 // get the first value 36 .005 37 + 38 1/X 39 100 40 * 41 INT // this is the min value the solution could be 42 RCL 10 43 .005 44 - 45 1/X 46 100 47 * 48 INT 49 1 50 + // and this is the max 51 1 E3 // OK, synthetic line would have been better! 52 / 53 + // this is an ISG template for possible solutions 54 STO 04 // store the template in Reg 04 55 FIX 2 // we need to round to 2 devimal places 56 END

And this is the main program

01 LBL "CD?"
02 RCL 04     // get template for possible solutions
03 STO 03     // Store in in Reg 03

04 LBL 01 // Start of routine to check if current value is OK 05 RCL 02 // get template for data registers 06 STO 01 // Reg 01 is our counter

07 LBL 02 // Start of routine to check a data value 08 XEQ 40 // check if current guess works with current data 09 X<>Y? // test returns Y as data, X as calc value 10 GTO 04 // Failed! Need to find a working value 11 TONE 9 // signals match of data works with guess 12 ISG 01 // now try next data 13 GTO 02 // loop back to check it 14 R^ // place guess in X 15 BEEP // Ta-Daaaaa! it worked!!! 16 STOP // t-t-t-that's all folks

17 LBL 03 // look at that, a routine that I don't use any more. 18 TONE 0 19 ISG 03 20 GTO 01

21 LBL 05 22 BEEP // TOTAL Failure 23 CLX // Place 0 in X 24 STOP

25 LBL 04 // Just increment guess until current data works with it 26 TONE 0 // Tone for failure of a guess 27 ISG 03 // Increment guess 28 GTO 06 // continue the test 29 GTO 05 // run off the end, so this is a failure

30 LBL 06 // check this guess with failed data item 31 XEQ 40 // do the check 32 X=Y? // this one OK? 33 GTO 07 // Yep!!!!! 34 GTO 04 // NO, sorry. Neet to try next guess

35 LBL 07 // Guess is OK for this data 36 TONE 7 // minor success sound 37 GTO 01 // now go back and check ALL the data

38 LBL 40 // check guess against data 39 RCL 03 40 INT // this is the guess 41 VIEW X // Show the poor user something other than a goose 42 RCL IND 01 // get data item 43 RCL Y // get guess 44 RCL Y // get data 45 * // about how many CDs? 46 100 47 / 48 .5 49 + 50 INT // how many CDs? 51 R^ // Guess 52 / // calculated proportion 53 100 54 * 55 RND // calculated percentage to 2 dp 65 RTN

OK, the idea is that the setup program assumes that the first data value is the smallest, and this percentage represents 1 set of CDs. So it determines a range of possible solutions

The main program loops through the possible solutions (guesses). It checks each data value in turn to see if this guess fits the data. If it's ok for all of them, then you're there, and the program finishes. If any data item fails, then the program keeps incrementing the guess until this data value becomes valid, then it returns to checking ALL the data values.

As I said. All in under 5 minutes. Note also that should the 0.12% represent 2 or more sets, then the routine would fail, but it would not be too hard for the routine to be extended to cater for that possibility too.

OK, as I said, I was in bed when I wrote this. I was so excited that I jumped up and I'm typing this dressed only in my underwear (not a pretty sight). I thank god for my UPS which does a good job of keeping the room warm :-)

What was that joke about middle aged men using the internet in their underwear? (oooh, yuk!)

Like I said -- Too much information :-)

            
Wow!
Message #4 Posted by Dave Hicks (USA) on 17 Sept 2000, 6:14 p.m.,
in response to message #3 by Steve (Australia)

Another impressive solution.

            
Re: This may be way too much information
Message #5 Posted by Viktor Toth on 17 Sept 2000, 10:39 p.m.,
in response to message #3 by Steve (Australia)

> I then decided to see if I could do better

Well, so can I! :-)

> the setup program [...] determines a range of possible solutions

That's cheating :-)

Now if you'd actually sort the data, find the smallest value, and then worked with that, I'd say that's nice. But assuming that the first one is the smallest? Since the values are hard coded anyway, why not just go ahead and plug in 800 [=int(1/(0.0012+0.00005))] as the starting value of the iteration? After all, that's precisely what your instructions 32-41 under label "S2" accomplish.

> should the 0.12% represent 2 or more sets, then the routine would fail

Well, you don't REALLY need to calculate a maximum value; why not just search until a solution is found? It is easy to see that one will be found, guaranteed (10,000 is a trivial solution.)

Anyway, I have here a somewhat optimized algorithm here that runs in about 4 minutes on a regular speed 42S, starting from 1. If I start from 800, it completes the run in less than 20 seconds.

The same algorithm on a blanknut 41CV runs to completion in less than 20 minutes with a starting value of 1. Starting with 800, the run time is around 1:30.

The beauty of this algorithm is that it doesn't use ANY registers; everything is done in the stack. As you can tell, I am a relative newcomer to RPN (my first RPN machine was an HP-28S a mere 12 years ago), so I fell in love with it even more :-)

> I'm typing this dressed only in my underwear

Er, I'm not sure I really wanted to know that. Let's just say that although this isn't necessarily always the case, I am presently fully clothed; and when I am not, I usually refrain from telling the world :-)

Viktor

 01 LBL "PERC"
 02 CLX		// or: 799
 03 ENTER^
 04 LBL 02
 05 CLX
 06 1
 07 +
 08 ENTER^	// You may insert a VIEW X here
 09 ENTER^
 10 ENTER^
 11.5546
 12 XEQ 01
 13 X!=0?
 14 GTO 02
 15 CLX
 16 .1022
 17 XEQ 01
 18 X!=0?
 19 GTO 02
 20 CLX
 21 .04
 22 XEQ 01
 23 X!=0?
 24 GTO 02
 25 CLX
 26 .0353
 27 XEQ 01
 28 X!=0?
 29 GTO 02
 30 CLX
 31 .0317
 32 XEQ 01
 33 X!=0?
 34 GTO 02
 35 CLX
 36 .0235
 37 XEQ 01
 38 X!=0?
 39 GTO 02
 40 CLX
 41 .0212
 42 XEQ 01
 43 X!=0?
 44 GTO 02
 45 CLX
 46 .0165
 47 XEQ 01
 48 X!=0?
 49 GTO 02
 50 CLX
 51 .0141
 52 XEQ 01
 53 X!=0?
 54 GTO 02
 55 CLX
 56 .0129
 57 XEQ 01
 58 X!=0?
 59 GTO 02
 60 CLX
 61 .0082
 62 XEQ 01
 63 X!=0?
 64 GTO 02
 65 CLX
 66 .0059
 67 XEQ 01
 68 X!=0?
 69 GTO 02
 70 CLX
 71 .0047
 72 XEQ 01
 73 X!=0?
 74 GTO 02
 75 CLX
 76 .0035
 77 XEQ 01
 78 X!=0?
 79 GTO 02
 80 CLX
 81 .0024
 82 XEQ 01
 83 X!=0?
 84 GTO 02
 85 CLX
 86 .0012
 87 XEQ 01
 88 X!=0?
 89 GTO 02
 90 RDN
 91 BEEP
 92 RTN
 93 LBL 01
 94 X
 95 LASTX
 96 1 E4
 97 X
 98 x<>y
 99 .5
100 +
101 INT
102 1 E4
103 X
104 R^
105 /
106 .5
107 +
108 INT
109 -
110 RTN
111 END


[ Return to Index | Top of Index ]

Go back to the main exhibit hall