(11C) 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: (11C) Digit Sum (/thread-9948.html) |
(11C) Digit Sum - Gamo - 01-16-2018 08:52 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 Code:
Gamo RE: (11C) Digit Sum - Dieter - 01-16-2018 07:16 PM Gamo, this is one of your best-coded 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 10x 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... (01-16-2018 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 RE: (11C) Digit Sum - wawa - 01-20-2018 08:14 AM 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:
RE: (11C) Digit Sum - Dieter - 01-20-2018 09:23 AM (01-20-2018 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 RE: (11C) Digit Sum - Gamo - 01-20-2018 10:10 AM 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 RE: (11C) Digit Sum - Dieter - 01-20-2018 05:21 PM (01-20-2018 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 four-digit 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 |