HP Forums
(11C) Sum of All Digits - 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) Sum of All Digits (/thread-11814.html)



(11C) Sum of All Digits - Gamo - 11-17-2018 08:22 AM

Program to Sum Digits and if the answer of the previous sum is over 9 then
sum again.

Example:

21579 = 2+1+5+7+9 = 24 --> 2+4 = 6

18235 = 1+8+2+3+5 = 19 --> 1+9 = 10 --> 1+0 = 1

987654321 ----> 9

-----------------------------------------------------------

Procedure:

Desire Digits [A] --> Answer Sum of All Digits
Continue with other digits with [R/S] or [A]

Program:
Code:

LBL A
ENTER
LOG
INT
9
-
10^X
÷
9
ENTER
ENTER
CLx
+
Rv
÷
INT
R^
x
-
X≠0
R/S
CLx
9
RTN

Gamo


RE: (11C) Sum of All Digits - Albert Chan - 11-17-2018 02:26 PM

Using LOG to scale away exponents assumed number > 0
So, mod 9 of scaled number can be simplified a bit

9 / FRAC 10 x INT


RE: (11C) Sum of All Digits - Thomas Klemm - 11-17-2018 02:51 PM

(11-17-2018 02:26 PM)Albert Chan Wrote:  9 / FRAC 10 x INT

Just don't use this for numbers bigger than 9ᴇ9.


RE: (11C) Sum of All Digits - Dieter - 11-17-2018 04:56 PM

(11-17-2018 02:26 PM)Albert Chan Wrote:  Using LOG to scale away exponents assumed number > 0

May I ask why the number is scaled at all?
Is a simple mod 9 not sufficient?

Also scaling by means of the base 10 log can be problematic. Try 999999999, the log of this is returned as exactly 9. And this is just a nine-digit number. Among the 10-digit numbers even log 9999999989 is calculated as exactly 10. So the scaling goes wrong. But I think it is not required anyway. Or is it? Gamo?

(11-17-2018 02:26 PM)Albert Chan Wrote:  So, mod 9 of scaled number can be simplified a bit

9 / FRAC 10 x INT

This would mean a program like this:

Code:
LBL A
9
÷
FRAC
1
0
x
INT
X=0?
9
RTN

Thomas said the max. input is 9 E+9, but actually the limit is even lower due to roundoff: try 8765432123. Dividing this by 9 yields 973936902,5555... so that the result should be 5. But with 10-digit accuracy this is rounded to 973936902,6 so the program returns 6 instead.

On the other hand the classic mod 9 implementation requires just one more step. It doesn't have the mentioned problem and it works for input up to 9 E+9.

Code:
LBL A
ENTER
ENTER
9
÷
INT
9
x
-
X=0?
9
RTN

8765432123  [A]  =>  5

BTW, unlike the original program this method also works on the 12C.

Dieter


RE: (11C) Sum of All Digits - Albert Chan - 11-17-2018 06:27 PM

(11-17-2018 04:56 PM)Dieter Wrote:  May I ask why the number is scaled at all?
Is a simple mod 9 not sufficient?

I think Gamo is trying to cover all ranges (except 0, or negative numbers).
I am not sure LOG can guarantee correct scaling though.

Example, LOG(99999 99991) = 10 (rounded from exact = 9.9999 99999 60913 ...)
Scaling it by 1/10^(10-9), we get X = 99999 9999.1
X % 9 returns weird sum of digits = 0.1 ???

Edit: scaling can still work, sum of digits = (X + 9*frac(X)) % 9
The correction fix possible INT LOG off-by-1 error.

(11-17-2018 04:56 PM)Dieter Wrote:  Thomas said the max. input is 9 E+9, but the limit is even lower due to roundoff errors ...

"9 / FRAC 10 x INT" idea is bad ...
In fact, this start failing even for 9 digits integer >= 9e8 + 5

Your mod9 code can handle full 10 digits integer (and a hair more, to 1e10)