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
Post Reply 


Messages In This Thread
HHC 2017 programming contest revisited - Don Shepherd - 11-30-2018 12:45 AM



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