Post Reply 
HHC 2019 will be in Reno, Nevada, Sept 21-22
09-28-2019, 01:26 AM (This post was last modified: 10-11-2019 01:06 AM by Albert Chan.)
Post: #52
RE: HHC 2019 will be in Reno, Nevada, Sept 21-22
HP71B version, with a running sum of number less cube of its digits.

If the total is zero, we found an Armstrong number.
Slight optimization on the inner loop. Instead of running from 0 to 9, do 2 to 9

If Armstrong number N last digit is 0, N+1 must also be an Armstrong number.

Code:
10 DESTROY ALL @ DIM C(10) @ FOR I=1 TO 9 @ C(I)=I^3-I @ NEXT I
20 FOR X=1 TO 9 @ M=99*X-C(X)
30 FOR Y=MOD(X,2) TO 9 STEP 2 @ N=M+9*Y-C(Y) @ IF N<=0 THEN EXIT Y
40 FOR Z=2 TO 9 @ IF N<=C(Z) THEN EXIT Z
50 NEXT Z
60 IF N=C(Z) THEN DISP X;Y;Z
70 NEXT Y
80 IF N=0 THEN DISP X;Y;0 @ DISP X;Y;1
90 NEXT X

Note: discovered a BASIC quirk

FOR NEXT loops variable ends with first value that failed the loop.
>FOR K=1 TO 100 STEP 7 @ NEXT K
>K
106

So, above code Z loop can also do 2 to 8, and still run correctly.

Update: Y loop optimized with parity test, cutting search space in half.
see https://www.hpmuseum.org/forum/thread-13791.html
Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
RE: HHC 2019 will be in Reno, Nevada, Sept 21-22 - Albert Chan - 09-28-2019 01:26 AM



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