The Museum of HP Calculators

HP Forum Archive 15

[ Return to Index | Top of Index ]

Re: Math Challenge #8:- HP-16C Version
Message #1 Posted by Bill (Smithville, NJ) on 29 Apr 2005, 1:14 p.m.

Hi Valentin,

I have always enjoyed your Math Challenges but until now have never actually tried programming one. When I first read this one, I starting thinking that some sort of bit shifting could be used to solve it. So I got my HP-16C out, loaded it with new batteries and sit down last night with the manual to refresh my memory of how it operated and could it be used to solve this. I came up with the following 84 step program:

001	LBL A
002	DEC     set to decimal
003	34
005	WSIZE   set wordsize to 34 bits
006	33333   starting number
011	STO 0
012	LBL B   main loop from 33333 to 999999
013	RCL 0
014	1
015	+       add one and save number
016	STO 0
017	99999
022	X <> Y
023	X > Y?
024	GTO E   all done
025	0
026	STO 4   Initilize R4 to zero bits
027	RCL 0
028	ENTER
029	*
030	STO 1   store square of number
031	10
033	STO I   store index
034	LBL C   loop over 10 digits
035	RCL 1
036	RCL 1
037	10
039	/
040	10
042	*
043	-       right most digit
044	X = 0?
045	GTO B   if zero, then exit this number
046	STO 2   save digit
047	10
049	STO 3	
050	LBL D   loop 9 to 1 to see which digit it is
051	RCL 3
052	1
053	-
054	STO 3
055	RCL 2
056	XOR
057	X NE 0?
058	GTO D   not this digit, go try next
059	1
060	RCL 3
061	RL N
062	RCL 4
063	OR      set bit in R4 to match digit found
064	STO 4
065	RCL 1
066	RCL 2
067	-
068	10
070	/       strip off right most digit
071	STO 1	
072	DSZ
073	GTO C   go try next digit
074	RCL 4   all digits scanned, recall bit register
075	#B      count number found
076	3
077	X NE Y?
078	GTO B   if not exactly 3, then go try next number in sequence
079	RCL 0
080	R/S     answer found - stop and display
081	GTO B	
082	LBL E   all done
083	0
084	R/S     stop and display zero

I've run it this morning and it found the first 4 solutions in a little over one hour. It's still working on finding the next solution.

I'm sure the program could be optimized, since it's been 20 years since I have actually programmed with it.

Thanks to you Valentin, I have had a lot of fun getting re-aquainted with an old friend, the HP-16C.

Bill

      
Re: Math Challenge #8:- HP-16C Version
Message #2 Posted by Don Shepherd on 30 Apr 2005, 10:58 a.m.,
in response to message #1 by Bill (Smithville, NJ)

Hey Bill, that's great work on the venerable 16c. You said you found 4 solutions, I thought that Valentin said that there was only one solution. I'm going to look at your code to get some insights myself. I think I'll try to write a program for the 12cp to solve it (since it's faster than the older voyagers).

            
Re: Math Challenge #8:- HP-16C Version
Message #3 Posted by Bill (Smithville) on 30 Apr 2005, 11:50 a.m.,
in response to message #2 by Don Shepherd

Hi Don,

My program looks for three unique digits, which I believe there are 47 solutions. I think there's only one solution for two unique digits. Not sure there's much insight in my program :) I'd be interested to see the 12CP version.. Not sure if the old 12C would have enough programming space to do it, but the 12cp has more space (?)

In looking over the command set for the 16C I just realized I could use RMD to isolate the right most digit. Would save a few steps.

If the base was switched from base ten to HEX or OCT, then the 16C would really shine. For example the square root of Hex number:

734877B53A10   is   ABCABC

and square root of the Oct Number: 14714571144 is 121212.

Using Hex or Oct, the bit operations would make the solution straight forward. I may try it just for fun.

Bill

                  
Re: Math Challenge #8:- HP-12C Version
Message #4 Posted by tony on 30 Apr 2005, 10:50 p.m.,
in response to message #3 by Bill (Smithville)

Bill, this 57 liner seems to do the job on the 12C. It takes 20-25 seconds to fully analyse a candidate though, and the user interface is not that friendly<G>. Cheers, Tony

R0 hits number : 0.300040003 ->only 1,5,9 in square
R1 33333 -> 
R2 10 (convenient constant)
R3 total of non-zero hits. If >3 we skip
R4 counter 0->10

Example: 33333 STO 1 R/S after 20 seconds see 3 rcl 1->33,334 enter * ->1,111,155,556 rcl 0->0.500041000 imagine 123456789 showing 5 1s, 4 5s and 1 6.

R/S, see 3, RCL 1 ->33,335 R/S 20-25 seconds, see 33,336... 33,337... 33,338 another stop etc.....

Keystrokes |Display | Comments [f][P/R] | | [f]CLEAR[PRGM] |00- | [EEX] |01- 26 | 1 |02- 1 | [STO]2 |03- 44 2 | R2=10 [CLx] |04- 35 | top of loop1 [STO]0 |05- 44 0 | initialize hits 1 |06- 1 | [STO][+]1 |07- 44 40 1 | [RCL]1 |08- 45 1 | [g][PSE] |09- 43 31 | show current candidate [ENTER] |10- 36 | [x] |11- 20 | [RCL]2 |12- 45 2 | top of loop2 [/] |13- 10 | [g][INTG] |14- 43 25 | keep for recycling [g][LSTx] |15- 43 36 | [g][FRAC] |16- 43 24 | [RCL]2 |17- 45 2 | [x] |18- 20 | digit found [g][x=0] |19- 43 35 | quick exit for 0 [g][GTO]04 |20- 43,33 04 | back to loop1 [CHS] |21- 16 | otherwise add [RCL]2 |22- 45 2 | 10^(-digit) to [x<>y] |23- 34 | the hits [y^x] |24- 21 | [STO][+]0 |25- 44 40 0 | [RDN] |26- 33 | finish if remnant [g][x=0] |27- 43 35 | is zero. Otherwise [g][GTO]30 |28- 43,33 30 | stay in loop2 [g][GTO]12 |29- 43,33 12 | [STO]3 |30- 44 3 | initialise digit [STO]4 |31- 44 4 | count & counter [RCL]0 |32- 45 0 | analyse hits [g][FRAC] |33- 43 24 | top of loop3 [RCL]2 |34- 45 2 | [x] |35- 20 | [g][INTG] |36- 43 25 | augment counter [STO][+]4 |37- 44 40 4 | [g][LSTx] |38- 43 36 | remnant [x<>y] |39- 34 | [g][x=0] |40- 43 35 | if no digits don't [g][GTO]45 |41- 43,33 45 | augment count 1 |42- 1 | [STO][+]3 |43- 44 40 3 | [RDN] |44- 33 | [RDN] |45- 33 | [RCL]2 |46- 45 2 | if 10 digits [RCL]4 |47- 45 4 | already found [-] |48- 30 | we are finished [g][x=0] |49- 43 35 | [g][GTO]53 |50- 43,33 53 | [RDN] |51- 33 | otherwise [g][GTO]33 |52- 43,33 33 | stay in loop3 3 |53- 3 | [RCL]3 |54- 45 3 | [g][x<=y] |55- 43 34 | stop if 3 or less [R/S] |56- 31 | different digits [g][GTO]04 |57- 43,33 04 | bottom of loop1 [f][P/R] | |

                        
Re: Math Challenge #8:- HP-12C Version
Message #5 Posted by Bill (Smithville, NJ) on 2 May 2005, 1:12 p.m.,
in response to message #4 by tony

Hi Tony,

Great to see that the HP-12C can also do this. When I get a moment of time, I'll program it in. Thanks for posting it.

Bill

      
Re: Math Challenge #8:- HP-16C Version
Message #6 Posted by Arnaud Amiel on 2 May 2005, 9:05 a.m.,
in response to message #1 by Bill (Smithville, NJ)

I was also thinking that byte shifting is the way to go but I don't own a 16C so I used saturn assembly on the 49. It is then very easy to program.

Arnaud

            
Re: Math Challenge #8:- HP-16C Version
Message #7 Posted by Bill (Smithville, NJ) on 2 May 2005, 1:15 p.m.,
in response to message #6 by Arnaud Amiel

Hi Arnaud,

Well, byte shifting didn't come into as much use as I originaly thought - since it was base 10. Now if it was BCD....

I'm still planning on doing it in another base, such as HEX or OCT. Then byte shifting would really work.

Bill

                  
Re: Math Challenge #8:- HP-16C Version
Message #8 Posted by Arnaud Amiel on 3 May 2005, 6:22 a.m.,
in response to message #7 by Bill (Smithville, NJ)

That is one good point of the saturn processor, I could code the whole thing in decimal mode.

Arnaud

      
Re: Math Challenge #8:- HP-16C Version
Message #9 Posted by Valentin Albillo on 4 May 2005, 4:41 a.m.,
in response to message #1 by Bill (Smithville, NJ)

Hi, Bill:

Bill posted:

"I have always enjoyed your Math Challenges but until now have never actually tried programming one."

Thanks for your interest and welcome to the club ! As you can see, the primary goal of my challenges is to provide fun, the kind of fun you get when you use your beloved calculator to try and find the answer to some interesting question, i.e.: to flex your 'calculateur extraordinaire' muscles, so to say. A secondary goal is to provide some didactic rewards in the process.

"I starting thinking that some sort of bit shifting could be used to solve it. So I got my HP-16C out, loaded it with new batteries and sit down last night with the manual to refresh my memory of how it operated and could it be used to solve this. I came up with the following 84 step program: [...] I'm sure the program could be optimized, since it's been 20 years since I have actually programmed with it."

Excellent attempt, indeed, and for an HP-16C no less ! There are so few programs out there for the 16C that I'm truly glad to see it used for this purpose. It is one thing to solve this challenge using and HP-71B or 42S, say, and quite another to do it with and HP-16C or 12C.

"Thanks to you Valentin, I have had a lot of fun getting re-aquainted with an old friend, the HP-16C."

You're welcome. And in more than one sense, hope to see you participating in my next challenges as well. You see, they aren't that difficult, they only seem to :-)

Best regards from V.


[ Return to Index | Top of Index ]

Go back to the main exhibit hall