(11C) Digit Sum

01162018, 08:52 AM
(This post was last modified: 01162018 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:
Gamo 

01162018, 07:16 PM
Post: #2




RE: (11C) Digit Sum
Gamo, this is one of your bestcoded 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 10^{x} 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... (01162018 08:52 AM)Gamo Wrote: In mathematics, the digit sum of a given integer is the sum of all its digits 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 

01202018, 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:
and here is a HP42s version of this program with a little modification in the way I use the stack Code:


01202018, 09:23 AM
Post: #4




RE: (11C) Digit Sum
(01202018 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" Note for 42s users: Y and L are stack registers (ST Y and ST L). Dieter 

01202018, 10:10 AM
(This post was last modified: 01202018 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:
Gamo 

01202018, 05:21 PM
(This post was last modified: 01202018 06:49 PM by Dieter.)
Post: #6




RE: (11C) Digit Sum
(01202018 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 fourdigit number, i.e. between 1000 and 9999, is generated this way: Code: RAN# Also, why do you waste another register (R2) for the digit sum? It is already stored in R1 (OK, 1/10 of it). Code: ... Every byte counts. ;) Dieter 

« Next Oldest  Next Newest »

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