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 // TaDaaaaa! it worked!!!
16 STOP // tttthat'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 :)
