(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 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 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 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 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 |
|||
« Next Oldest | Next Newest »
|
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)