Post Reply 
(15C) Find narcissistic / Armstrong numbers
02-28-2024, 04:15 PM (This post was last modified: 03-01-2024 05:26 PM by jonakeys.)
Post: #1
(15C) Find narcissistic / Armstrong numbers
Hi everyone,

For a couple of weeks I've added a HP-15C CE to my calculator collection. As I like to program as a hobby, I found a nice challenge in the archive: "Short & Sweet Math Challenges for HP fans #3" Posted by Ex-PPC member on 9 June 2002, 12:43 a.m.
The goal is to find Narcissistic or Armstrong numbers, for example: 1^3+5^3+3^3=153
Finding the 4 numbers (and finishing the program) for 3 digits takes around 34 seconds.

How to use:
Enter an integer (N) and press f LBL A to start the search. When a valid number is found, it is added to matrix A. The display shows 0.0000 when finished.

Labels
A: Start of program
B: Fill storage registers (x^N)
C: Calculate value of number
D: Iterate over digits
E: End

Storage registers
R1-R9: x^N
R.0: N digits
R.1: Loop from
R.2: Loop to
R.3: Sum number
R.4: Working number

Matrix
A: Result numbers

I've posted the code also on Github https://github.com/jonakeys/HP-15C

I'm sure it can be faster/more efficient, so if you see room for improvement, feel free to comment so I can learn!

Code:

| 000 | *Keystrokes* | *Key Codes* | *Comments*                              |
| 001 | f LBL A      | 42,21,11    | A: Start of program                     |
| 002 | STO .0       | 44,.0       | Store N digits in .0                    |
| 003 | 10^x         | 13          |                                         |
| 004 | STO .2       | 44,.2       | Set loop to: 10^N (excl)                |
| 005 | g LSTx       | 43,36       |                                         |
| 006 | 1            | 1           |                                         |
| 007 | -            | 30          |                                         |
| 008 | 10^x         | 13          |                                         |
| 009 | STO .1       | 44,.1       | Set loop from: 10^(N-1)                 |
| 010 | 1            | 1           | Loop from 1 to 9                        |
| 011 | .            | 48          |                                         |
| 012 | 0            | 0           |                                         |
| 013 | 0            | 0           |                                         |
| 014 | 9            | 9           |                                         |
| 015 | STO I        | 44,25       |                                         |
| 016 | f LBL B      | 42,21,12    | Fill R1-R9: x^N                         |
| 017 | RCL I        | 45,25       |                                         |
| 018 | g INT        | 43,44       |                                         |
| 019 | RCL .0       | 45,.0       |                                         |
| 020 | y^x          | 14          |                                         |
| 021 | STO f (i)    | 44,24       |                                         |
| 022 | f ISG I      | 42,6,25     |                                         |
| 023 | GTO B        | 22,12       |                                         |
| 024 | 1            | 1           | Prepare results matrix                  |
| 025 | ENTER        | 36          |                                         |
| 026 | 9            | 9           |                                         |
| 027 | f DIM A      | 42,23,11    |                                         |
| 028 | 0            | 0           |                                         |
| 029 | STO MATRIX A | 44,16,11    | [x,x,x,x,x,x,x,x,x]                     |
| 030 | 1            | 1           |                                         |
| 031 | STO - .1     | 44,30,.1    |                                         |
| 032 | f LBL C      | 42,21,13    | Calculate value of number               |
| 033 | 1            | 1           |                                         |
| 034 | STO + .1     | 44,40,.1    | Increase .1 by 1 (next number)          |
| 035 | RCL .2       | 45,.2       | .2 = loop to                            |
| 036 | RCL .1       | 45,.1       | .1 = loop from                          |
| 037 | g TEST 7     | 43,30,7     | If from>to: Done                        |
| 038 | GTO E        | 22,15       |                                         |
| 039 | RCL .1       | 45,.1       |                                         |
| 040 | STO .4       | 44,.4       | .4 = working number                     |
| 041 | g CLX        | 43,35       | Clear sum                               |
| 042 | STO .3       | 44,.3       | .3 = sum                                |
| 043 | f LBL D      | 42,21,14    | Iterate over N digits                   |
| 044 | RCL .4       | 45,.4       |                                         |
| 045 | g INT        | 43,44       |                                         |
| 046 | g TEST 4     | 43,30,4     | Working number <=0: Move to next number |
| 047 | GTO .9       | 22,.9       |                                         |
| 048 | 1            | 1           | Get next digit by MOD                   |
| 049 | 0            | 0           |                                         |
| 050 | ÷            | 10          |                                         |
| 051 | STO .4       | 44,.4       |                                         |
| 052 | g LSTx       | 43,36       |                                         |
| 053 | x<>y         | 34          |                                         |
| 054 | f FRAC       | 42,44       |                                         |
| 055 | x            | 20          |                                         |
| 056 | g TEST 4     | 43,30,4     | Skip to next digit if digit<=0          |
| 057 | GTO D        | 22,14       |                                         |
| 058 | g INT        | 43,44       |                                         |
| 059 | STO I        | 44,25       | Store current digit in I                |
| 060 | 1            | 1           |                                         |
| 061 | g TEST 6     | 43,30,6     | If I!=1: get from Rx                    |
| 062 | RCL (i)      | 45,24       | Get stored value from Rx (x^N)          |
| 063 | STO + .3     | 44,40,.3    | Add value to sum                        |
| 064 | RCL .1       | 45,.1       |                                         |
| 065 | RCL .3       | 45,.3       |                                         |
| 066 | g TEST 7     | 43,30,7     | If sum>number: go to next number        |
| 067 | GTO C        | 22,13       |                                         |
| 068 | GTO D        | 22,14       |                                         |
| 069 | f LBL .9     | 42,21,.9    |                                         |
| 070 | RCL .1       | 45,.1       |                                         |
| 071 | RCL .3       | 45,.3       |                                         |
| 072 | g TEST 5     | 43,30,5     | Check if sum = number                   |
| 073 | u STO A      | u,44,11     | Store result in matrix if equal         |
| 074 | GTO C        | 22,13       |                                         |
| 075 | f LBL E      | 42,21,15    | End (return)                            |
| 076 | g CLX        | 43,35       | Clear x                                 |
| 077 | f MATRIX 1   | 42,16,1     | Set matrix to first position 1,1        |
| 078 | g RTN        | 43,32       |                                         |

edit: Add instructions of how to use

/ Jonathan
HP 15C CE, HP 35S, HP Prime G2, Casio Graph 90+E, TI Nspire CX, TI 83, TI 84+, TI 84+ CE
Visit this user's website Find all posts by this user
Quote this message in a reply
02-28-2024, 11:43 PM
Post: #2
RE: (15C) Find narcissistic / Armstrong numbers
.
Hi, jonakeys,

Welcome to the MoHPC forum !

I am the one who posted that S&SMC #3 so thank you for your interest in my production of some 22 years ago.

If you'd like to see many more of my challenges (and in particular many HP-15C mini-challenges,) you can find them all in the HP Calculator Challenges Section at my site.

Quote:I'm sure it can be faster/more efficient, so if you see room for improvement, feel free to comment so I can learn!

The thread for my S&SMC #3 contains tons of solutions and comments on techniques to optimize the search, so you can try those ideas, collected in this 20-page PDF document.

One of these days I might post an UBASIC program I wrote at the time which can find all such numbers up to 39 digits long in a very few hours on pretty ancient hardware.

Again, welcome and best regards.

V.

  
All My Articles & other Materials here:  Valentin Albillo's HP Collection
 
Visit this user's website Find all posts by this user
Quote this message in a reply
02-29-2024, 04:43 PM
Post: #3
RE: (15C) Find narcissistic / Armstrong numbers
Thanks for your kind words! And it's great that you are still around after these years and responded. I really like challenges and exercises so I'll definitely going to try more of them. Thanks for the links, I've taken a quick look and that seems promising. A lot of gathered information to dive in to.
And I'll keep my eyes open for more programs that may appear.

What I like about the calculators is that even though it's more cumbersome and restricted than using a PC and programming languages, they're very capable. And limitations call for for creative solutions and thinking.

/ Jonathan
HP 15C CE, HP 35S, HP Prime G2, Casio Graph 90+E, TI Nspire CX, TI 83, TI 84+, TI 84+ CE
Visit this user's website Find all posts by this user
Quote this message in a reply
03-01-2024, 07:20 AM (This post was last modified: 03-01-2024 01:08 PM by jonakeys.)
Post: #4
RE: (15C) Find narcissistic / Armstrong numbers
By the way; the time it takes in seconds to calculate the answers on the HP-15C CE let's describe itself by the formula x^1.07x pretty accurately. At least for the first 5 numbers.

/ Jonathan
HP 15C CE, HP 35S, HP Prime G2, Casio Graph 90+E, TI Nspire CX, TI 83, TI 84+, TI 84+ CE
Visit this user's website Find all posts by this user
Quote this message in a reply
03-01-2024, 03:29 PM
Post: #5
RE: (15C) Find narcissistic / Armstrong numbers
Question: Is step 47 supposed to be GTO C or GTO .9?

Also, I get all zeros in Matrix A.
Visit this user's website Find all posts by this user
Quote this message in a reply
03-01-2024, 05:31 PM
Post: #6
RE: (15C) Find narcissistic / Armstrong numbers
(03-01-2024 03:29 PM)Eddie W. Shore Wrote:  Question: Is step 47 supposed to be GTO C or GTO .9?

Also, I get all zeros in Matrix A.

Thanks for noticing! You're right, step 47 is supposed to be "GTO .9". The keystroke was right, the key code wasn't.
I've checked all steps with the program running on my device and all strokes+codes are correct now.

Also be aware that line 73 has a 'u' in front so you have to press "f USER" before entering that line.

After running the result must be in Matrix A. The program sets itself to location 1,1. When using "f USER" (42,45) you can press RCL (45) and the A-key (11) to show the number and move to the next one.

Hopefully it will work this way!

/ Jonathan
HP 15C CE, HP 35S, HP Prime G2, Casio Graph 90+E, TI Nspire CX, TI 83, TI 84+, TI 84+ CE
Visit this user's website Find all posts by this user
Quote this message in a reply
Post Reply 




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