HP Forums
(12C) Digit Sum - Printable Version

+- HP Forums (https://www.hpmuseum.org/forum)
+-- Forum: HP Software Libraries (/forum-10.html)
+--- Forum: General Software Library (/forum-13.html)
+--- Thread: (12C) Digit Sum (/thread-10656.html)



(12C) Digit Sum - Gamo - 05-03-2018 10:50 AM

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
Register used R1, R2 and R3
84001 R/S > 13

Code:

01 STO 1
02   1
03   0
04 STO 3
05   /
06 FRAC
07 RCL 3
08   x
09 STO+2
10 RCL 1
11 RCL 3
12   /
13 INTG
14 STO 1
15 X=0 ?
16 GTO 18
17 GTO 01
18 RCL 2
19   0
20 STO 2
21 X<>Y


Gamo


RE: (12C) Digit Sum - Dieter - 05-03-2018 06:02 PM

(05-03-2018 10:50 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
Register used R1, R2 and R3

Three data registers for such a simple task – Gamo, in January you posted a much better digit sum program for the 11C. With a simple change (X≠0? is not available) it can also be used on the 12C.

BTW, in the current program you should clear R2 before (!) the program sums up the digits there, and not at the end. This can be done without disturbing the stack with a STO 2 STO–2 sequence.

Dieter


RE: (12C) Digit Sum - Dieter - 05-04-2018 12:11 PM

(05-03-2018 10:50 AM)Gamo Wrote:  In mathematics, the digit sum of a given integer is the sum of all its digits
....
Code:
01 STO 1
02   1
03   0
04 STO 3
05   /
06 FRAC
07 RCL 3
08   x
.. ...

Gamo, as already noted you posted a much better program a few month ago. I would be glad if I could help you improve your programming skills, so let me show how to develop a more effective program.

First of all, the program has to split off the last digit from the number and leave the remaining part. Your program requires one data register only to store the 10 in there. The RCL 3 in line 11 could be replaced by LstX, so there are only two occurences of this 10, and one STO and another RCL require the same two steps as a simple second "10". So nothing is saved here.

But there is a better method for splitting of a digit from a number. It has been used in some earlier examples, maybe you didn't notice. Assume the number 1234 is stored in R1:

Code:
  1
  0
STO/1   1234 in R1 becomes 123,4
RCL 1   recalls 123,4 (note the 10 is still in Y)
FRAC    = 0,4
STO-1   123,4 in R1 becomes 123
  x     0,4 x 10 = 4

This routine only requires two stack levels to that Z and T are preserved. So there is no need to carry the digit sum in a separate register – just keep it on the stack. Start with 0, split off the digit and add it with a simple "+".

This way the program becomes...

Code:
01  STO 1
02    0
03  ENTER
04    1
05    0
06  STO/1   1234 in R1 becomes 123,4
07  RCL 1   recalls 123,4 (the 10 is still in Y)
08  FRAC    0,4
09  STO-1   123,4 in R1 becomes 123
10    x     0,4 x 10 = 4
11    +     add digit to sum
..  ...

The ENTER is only required to separate the "0" from the "10".

Finally we have to check if the remaining number in R1 has become zero. The number, or 1/10 of it, is recalled in line 07. So we can put the test there:

Code:
01  STO 1   store number in R1
02    0     start sum with 0
03  ENTER
04    1
05    0
06  STO/1   1234 in R1 becomes 123,4
07  RCL 1   recalls 123,4 (the 10 is still in Y)
08  X=0?    (is the remaining number = 0 ?)
09  GTO xx  (then exit)
10  FRAC    123,4 becomes 0,4
11  STO-1   123,4 in R1 becomes 123,4 - 0,4 = 123
12    x     0,4 x 10 = 4
13    +     add digit to sum
14  GTO 04  get next digit
15  R↓      get digit sum
16  R↓      which was in Z
17  GTO 00  and quit

I hope this more detailled explanation helps a bit.

BTW, on exit the original number is still available in Y. ;-)

84001 [R/S] => 13
[X<>Y] => 84001

If you replace the two R↓ with "x +" the number on exit even fills Y, Z and T.

Dieter


RE: (12C) Digit Sum - Gamo - 05-10-2018 12:11 PM

Dieter Thank You for the better solution.

A little update: Very short program on "Two Digits Sum"

Code:

1
0
%
INTG
9
x
-

34 R/S > 7
56 R/S > 11

Gamo Wink


RE: (12C) Digit Sum - Gamo - 01-08-2019 04:34 AM

Simple straight forward approach for Digit Sum

Example:

101 [R/S] display 2

Code:

01 STO 0
02  0
03 STO 1
04 RCL 0
05  1
06  0
07  ÷
08 STO 0
09 X=0
10 GTO 20
11 RCL 0
12 FRAC
13  -
14 STO 0
15 LSTx
16 RCL 1
17  +
18 STO 1
19 GTO 04
20 RCL 1
21  1
22  0
23  x

Gamo


RE: (12C) Digit Sum - Thomas Klemm - 01-08-2019 08:24 AM

(01-08-2019 04:34 AM)Gamo Wrote:  Simple straight forward approach for Digit Sum

For a slightly different approach:
Code:
01-    44 0    STO 0
02-       1    1
03-       0    0
04-      10    ÷
05-   43 25    INTG
06-   43 35    x=0
07-43,33 14    GTO 14
08-       9    9
09-      34    x<>y
10-      20    ×
11- 44 30 0    STO- 0
12-   43 36    LSTx
13-43,33 02    GTO 02
14-    45 0    RCL 0

Example:

4711
R/S
13


The program calculates: 4711 - 9×471 - 9×47 - 9×4 = 13

Or a bit longer without use of registers:
Code:
01-      36    ENTER
02-      36    ENTER
03-      35    CLx
04-      34    x<>y
05-       1    1
06-       0    0
07-      10    ÷
08-   43 25    INTG
09-   43 35    x=0
10-43,33 14    GTO 14
11-      40    +
12-   43 36    LSTx
13-43,33 05    GTO 05
14-      33    R↓
15-       9    9
16-      20    ×
17-      30    -

Here it calculates: 4711 - 9×(471 + 47 + 4) = 13


Cheers
Thomas

PS: Just noticed that x=0 doesn't enable stack-lift.
Is that well known?