Post Reply 
HHC 2017 programming contest revisited
11-30-2018, 12:45 AM (This post was last modified: 11-30-2018 03:28 AM by Don Shepherd.)
Post: #1
HHC 2017 programming contest revisited
Recall the programming contest for last year's HHC-2017 conference regarding finding "happy" and "unhappy" numbers. I recently acquired a Sharp PC-1211 Pocket Computer (vintage 1980) which runs BASIC. I decided to write a happy number finder for it. Then I decided to write an equivalent RPN routine for my 12c (vintage 2001, not the much faster 12c+). Then I ran several tests with the machines side-by-side so I could compare the speeds. Amazingly enough, each machine took EXACTLY the same amount of time when determining whether a number was happy or unhappy. So BASIC vintage 1980 was equivalent to RPN vintage 2001. Very interesting.

Here is the code.

Code:

First, the BASIC code for the Sharp PC-1211

A=current number
B=cycles
C=sum of squares
D=digit
I=loop index

10 "Z":AREAD A:B=0
20 C=0:B=B+1
30 FOR I=0 TO LOG(A)
40 D=A-10 x INT(A/10)
50 A=INT(A/10)
60 C=C+D*D
70 NEXT I
80 IF C=1 PAUSE "HAPPY CYC=",B:END
90 IF C=4 PAUSE "UNHAPPY CYC=",B:END
100 A=C:GOTO 20



Now, the RPN code for the 12c

R1=current number
R2=sum of squares
R3=cycles

when program completes, happy (1) or unhappy (4) is shown, X<->Y shows cycles

01 STO 1
02 0
03 STO 3
04 STO 2
05 RCL 1
06 .
07 1
08 x
09 INT
10 STO 1
11 LSTX
12 FRAC
13 ENTER
14 x
15 STO+2
16 RCL 1
17 X=0
18 GOTO 20
19 GOTO 06
20 1
21 STO+3
22 RCL 2
23 1
24 0
25 0
26 x
27 STO 1
28 1
29 -
30 X=0
31 GOTO 39
32 RCL 1
33 4
34 -
35 X=0
36 GOTO 39
37 0
38 GOTO 04
39 RCL 3
40 LSTX
Find all posts by this user
Quote this message in a reply
11-30-2018, 12:48 AM
Post: #2
RE: HHC 2017 programming contest revisited
Thank you for this. Glad you thought of it again.

Gene
Find all posts by this user
Quote this message in a reply
11-30-2018, 01:59 AM
Post: #3
RE: HHC 2017 programming contest revisited
Very nice program.
What if 100 change to EEX 2 is this help speed up the 12C at bit?

Gamo
Find all posts by this user
Quote this message in a reply
11-30-2018, 02:37 AM
Post: #4
RE: HHC 2017 programming contest revisited
(11-30-2018 01:59 AM)Gamo Wrote:  Very nice program.
What if 100 change to EEX 2 is this help speed up the 12C at bit?

Gamo
Thanks Gamo. I tried that and it had no obvious effect. There might be some microscopic time improvement of EEX 2 versus 100, but the number of iterations for this program loop is so small there is no obvious improvement in speed for the 12c.

Don
Find all posts by this user
Quote this message in a reply
11-30-2018, 04:30 PM
Post: #5
RE: HHC 2017 programming contest revisited
(11-30-2018 01:59 AM)Gamo Wrote:  What if 100 change to EEX 2 is this help speed up the 12C at bit?

Since R2 won’t contain 0, I would suggest replacing 1 0 0 with ENTER %T. Not noticeably any faster, though.
Find all posts by this user
Quote this message in a reply
Post Reply 




User(s) browsing this thread: 1 Guest(s)