(12C) Digit Sum
|
05-03-2018, 10:50 AM
(This post was last modified: 05-03-2018 11:47 AM by Gamo.)
Post: #1
|
|||
|
|||
(12C) 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 Register used R1, R2 and R3 84001 R/S > 13 Code:
Gamo |
|||
05-03-2018, 06:02 PM
(This post was last modified: 05-03-2018 06:05 PM by Dieter.)
Post: #2
|
|||
|
|||
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 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 |
|||
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 |
|||
05-10-2018, 12:11 PM
Post: #4
|
|||
|
|||
RE: (12C) Digit Sum
Dieter Thank You for the better solution.
A little update: Very short program on "Two Digits Sum" Code:
34 R/S > 7 56 R/S > 11 Gamo |
|||
01-08-2019, 04:34 AM
Post: #5
|
|||
|
|||
RE: (12C) Digit Sum
Simple straight forward approach for Digit Sum
Example: 101 [R/S] display 2 Code:
Gamo |
|||
01-08-2019, 08:24 AM
Post: #6
|
|||
|
|||
RE: (12C) Digit Sum
(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 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 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? |
|||
« Next Oldest | Next Newest »
|
User(s) browsing this thread: 1 Guest(s)