(11C) Sum of All Digits
|
11-17-2018, 08:22 AM
Post: #1
|
|||
|
|||
(11C) Sum of All Digits
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:
Gamo |
|||
11-17-2018, 02:26 PM
Post: #2
|
|||
|
|||
RE: (11C) Sum of All Digits
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 |
|||
11-17-2018, 02:51 PM
Post: #3
|
|||
|
|||
RE: (11C) Sum of All Digits | |||
11-17-2018, 04:56 PM
(This post was last modified: 11-17-2018 05:56 PM by Dieter.)
Post: #4
|
|||
|
|||
RE: (11C) Sum of All Digits
(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 This would mean a program like this: Code: LBL A 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 8765432123 [A] => 5 BTW, unlike the original program this method also works on the 12C. Dieter |
|||
11-17-2018, 06:27 PM
(This post was last modified: 11-17-2018 07:44 PM by Albert Chan.)
Post: #5
|
|||
|
|||
RE: (11C) Sum of All Digits
(11-17-2018 04:56 PM)Dieter Wrote: May I ask why the number is scaled at all? 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) |
|||
« Next Oldest | Next Newest »
|
User(s) browsing this thread: 6 Guest(s)