Post Reply 
(12C) Digit Sum
05-04-2018, 12:11 PM
Post: #3
RE: (12C) Digit Sum
(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
Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
(12C) Digit Sum - Gamo - 05-03-2018, 10:50 AM
RE: (12C) Digit Sum - Dieter - 05-03-2018, 06:02 PM
RE: (12C) Digit Sum - Dieter - 05-04-2018 12:11 PM
RE: (12C) Digit Sum - Gamo - 05-10-2018, 12:11 PM
RE: (12C) Digit Sum - Gamo - 01-08-2019, 04:34 AM
RE: (12C) Digit Sum - Thomas Klemm - 01-08-2019, 08:24 AM



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