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


Messages In This Thread
(15C) Find narcissistic / Armstrong numbers - jonakeys - 02-28-2024 04:15 PM



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