Post Reply 
(11C) Digit Sum
01-16-2018, 08:52 AM (This post was last modified: 01-16-2018 08:53 AM by Gamo.)
Post: #1
(11C) Digit Sum
In mathematics, the digit sum of a given integer is the sum of all its digits
The digit sum of 84001 is calculated as 8+4+0+0+1 = 13

Code:

LBL A
0
STO 1
R↓
LBL 1
1
0
÷
FRAC
STO+1
LSTx
INT
X≠0
GTO 1
10^x
10^x
RCL 1
x
RTN

Gamo
Find all posts by this user
Quote this message in a reply
01-16-2018, 07:16 PM
Post: #2
RE: (11C) Digit Sum
Gamo, this is one of your best-coded programs. And it uses a clever algorithm: it sums 1/10 of the digits and finally multiplies the result by 10. Which is obtained by two consecutive 10x commands (0 → 1 → 10).

I like such little tricks (altough a plain 10 is faster and doesn't require more steps). Let me add one more: If you want to zero data register 1 without affecting the stack, a simple STO 1 STO–1 does it. Saves one step here.

By the way...

(01-16-2018 08:52 AM)Gamo Wrote:  In mathematics, the digit sum of a given integer is the sum of all its digits
The digit sum of 84001 is calculated as 8+4+0+0+1 = 13

And the digit sum of this (the digital root) is 4 (simply run the program once more).
But you can calculate this directly, it's simply n mod 9. Or 9 if this yields zero (and n≠0).

Dieter
Find all posts by this user
Quote this message in a reply
01-20-2018, 08:14 AM
Post: #3
RE: (11C) Digit Sum
I like this program very much too.

I've made a little modification to operate only in the stack, avoiding the use of the register 1.
Code:

LBL A
0
X<>Y
LBL 0
1
0
÷
FRAC
LST X
R↓
+
R^
INT
X̣!=0?
GTO 0
R↓
1
0
x
RTN

and here is a HP42s version of this program with a little modification in the way I use the stack
Code:

00 { 38-Byte Prgm }
01▸LBL "SumDg"
02 0
03 10
04▸LBL 00
05 STO÷ ST Z
06 RCL ST Z
07 IP
08 X=0?
09 GTO 01
10 X<> ST T
11 FP
12 STO+ ST Z
13 R↓
14 GTO 00
15▸LBL 01
16 R↓
17 X<> ST Z
18 +
19 ×
20 RTN
21 END
Find all posts by this user
Quote this message in a reply
01-20-2018, 09:23 AM
Post: #4
RE: (11C) Digit Sum
(01-20-2018 08:14 AM)wawa Wrote:  and here is a HP42s version of this program with a little modification in the way I use the stack
...

This one is shorter and requires less stack acrobatics. Works on the 41 and the 42s and also preserves Y.

Code:
01 LBL"DSUM"
02 0
03 X<>Y
04 LBL 01
05 10
06 /
07 FRC
08 ST+ Y
09 X<> L
10 INT
11 X≠0?
12 GTO 01
13 +
14 10
15 *
16 END

Note for 42s users: Y and L are stack registers (ST Y and ST L).

Dieter
Find all posts by this user
Quote this message in a reply
01-20-2018, 10:10 AM (This post was last modified: 01-20-2018 11:57 AM by Gamo.)
Post: #5
RE: (11C) Digit Sum
Here is a "4 Digits Random Number to the Digit Sum" for Practice adding digits.

To Run:
Press A and calculator briefly show 4 digits number then return 0.
Try to add the digits in your head and check to see the number and answer.

Press R/S shown number and X<>Y show answer. Press X<>Y again will cycle to number and answer.

Program:
Code:

LBL A
FIX 0
RAN#
EEX
4
x
INT
STO 0
PSE
CLx
RCL 0
0
STO 1
Roll Down
LBL 1
10
/
FRAC
STO+1
LSTx
INT
X≠0 (TEST 0)
GTO 1
10^x
10^x
RCL 1
x
STO 2
CLx
R/S
RCL 2
RCL 0
R/S

Gamo
Find all posts by this user
Quote this message in a reply
01-20-2018, 05:21 PM (This post was last modified: 01-20-2018 06:49 PM by Dieter.)
Post: #6
RE: (11C) Digit Sum
(01-20-2018 10:10 AM)Gamo Wrote:  Press A and calculator briefly show 4 digits number ...

The program generates a number between 0 and 9999, so this number may have one, two, three or four digits. A four-digit number, i.e. between 1000 and 9999, is generated this way:

Code:
RAN#
9
EEX
3
x
INT
EEX
3
+

Also, why do you waste another register (R2) for the digit sum? It is already stored in R1 (OK, 1/10 of it).

Code:
...
GTO 1
1
0
ST0x1
CLx
R/S
RCL 1
RCL 0
RTN

Every byte counts. ;-)

Dieter
Find all posts by this user
Quote this message in a reply
Post Reply 




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