Sum of Digits. - Printable Version +- HP Forums (https://www.hpmuseum.org/forum) +-- Forum: Not HP Calculators (/forum-7.html) +--- Forum: Not quite HP Calculators - but related (/forum-8.html) +--- Thread: Sum of Digits. (/thread-5928.html) Pages: 1 2 |
RE: Sum of Digits. - fhub - 03-26-2016 06:17 PM (03-26-2016 06:02 PM)Dieter Wrote: Four steps is easy – one example is your solution, another one is...Yes, I also found this solution, but it doesn't work for 0 (whereas the other one does). Quote:So four steps is trivial. For less it takes some real art of programming.Well, it also doesn't mean that it does exist. ;-) I doubt that it can really be reduced by one step. Franz RE: Sum of Digits. - ggauny@live.fr - 03-26-2016 06:22 PM Do you use EXPT, INC x in your solution ? RE: Sum of Digits. - Dieter - 03-26-2016 06:27 PM (03-26-2016 06:22 PM)ggauny@live.fr Wrote: Do you use EXPT, INC x in your solution ? No, it's much easier than that. Take a look at the links in post #20. Dieter RE: Sum of Digits. - ggauny@live.fr - 03-26-2016 08:15 PM Hi, After seeying post #20 link in french, I am as before. I think it is very simple solution but only for you. Of course you have the solution of this challenge. Me I will try again, with eyes I see how to do, but difficult to make the wp34s see with eyes. 15 : 1 5 I add and it is 6. In 3 steps ???? RE: Sum of Digits. - ggauny@live.fr - 03-26-2016 08:18 PM And if 1 5 6 it is 12 And etc ! In sudoku it is noted "diabolic" ! RE: Sum of Digits. - Dieter - 03-26-2016 09:06 PM (03-26-2016 08:15 PM)ggauny@live.fr Wrote: After seeying post #20 link in french, I am as before. The solution is simple because it is just x mod 9. ;-) (Or 9 if this yields zero). x=156: 1+5+6 = 12. Then 1+2 = 3 156 mod 9 = 3 x=12345: 1+2+3+4+5 = 15. Then 1+5 = 6 12345 mod 9 = 6 x=987654321: 9+8+7+6+5+4+3+2+1 = 45. Then 4+5 = 9 987654321 mod 9 = 0 => result is 9 Voilà – it's as simple as that. (03-26-2016 08:15 PM)ggauny@live.fr Wrote: 15 : 1 5 I add and it is 6. 15 mod 9 = 6 (03-26-2016 08:15 PM)ggauny@live.fr Wrote: In 3 steps ???? Well... OK, that's a real challenge. ;-) But it can be done in four steps. Franz used the more correct solution 1 + (x–1) mod 9 which can be coded in four steps as well. At least on the 34s. Dieter RE: Sum of Digits. - ggauny@live.fr - 03-27-2016 07:10 AM Well, well, well, now I understand ! Modulation ways is helpfull here. Thank you and Franz. RE: Sum of Digits. - ggauny@live.fr - 03-29-2016 11:34 AM Hi, I think this little routine give the answer of the Dieter's challenge : Code:
It run. Have a nice day. RE: Sum of Digits. - Dieter - 03-29-2016 07:36 PM (03-29-2016 11:34 AM)ggauny@live.fr Wrote: It run. Gérard, have you really entered and run this program? I would be surprised if it returned anything else but 1 – for any input. ;-) These steps... Code: ENTER ...actually calculate \(\frac{x}{10^{log x - 9}} = \frac{x}{x \cdot 10^{-9}} = 10^{9}\) And 109 mod 9 = 1. Dieter RE: Sum of Digits. - ggauny@live.fr - 03-30-2016 10:00 AM Hi, Well catch as say Marcus, Yes it run but I have omited step 04 it is IP. I scuse me for my omit. Thank you for remarq. I promis you that it run on my hp41c and CX and WP34s. RE: Sum of Digits. - ggauny@live.fr - 03-30-2016 10:04 AM I have modified my first post, then now it run the routine ! Thank's to Dieter ! RE: Sum of Digits. - BarryMead - 03-30-2016 11:11 AM (03-26-2016 01:02 PM)fhub Wrote:This version works great on POSITIVE values but for negative values it returns incorrect answers when (X MOD 9) is -8 or Zero. I haven't got a fix yet, but I thought you would like to know that it isn't quite right. RE: Sum of Digits. - BarryMead - 03-30-2016 11:24 AM (03-26-2016 06:02 PM)Dieter Wrote: Four steps is easy – one example is your solution, another one is...In addition to handling Zero incorrectly, this version also has a problem when the value is negative if (X MOD 9 = 0) returns Positive 9 instead of -9 as it should. RE: Sum of Digits. - Dieter - 03-30-2016 12:42 PM (03-30-2016 10:00 AM)ggauny@live.fr Wrote: Well catch as say Marcus, Yes it run but I have omited step 04 it is IP. This way the entered number is filled up with zeroes until it has 10 digits. 123 => 1230000000 12345 => 1234500000 12345678 => 1234567800 Adding zeros will of course not change the digit sum. But it does not make any sense either. So why do you do this? Dieter RE: Sum of Digits. - Dieter - 03-30-2016 12:46 PM (03-30-2016 11:24 AM)BarryMead Wrote:(03-26-2016 06:02 PM)Dieter Wrote: Four steps is easy – one example is your solution, another one is...In addition to handling Zero incorrectly, this version also has a problem when the value is negative if (X MOD 9 = 0) returns Positive 9 instead of -9 as it should. That's why my post said "Well, at least for x>0". I wonder how digit sums are defined for negative values. Would –12345 yield –6? In this case a solution is easy: Code: SIGN OK, that's eight steps. Dieter RE: Sum of Digits. - ggauny@live.fr - 03-30-2016 05:37 PM On my hp 41 I have not 00000, For instance 12345 is displayed 12345 and this give me 6, in FIX 0, or 6,00000 in FIX 5. May be I have bad understood your question ? RE: Sum of Digits. - Dieter - 03-30-2016 06:01 PM (03-30-2016 05:37 PM)ggauny@live.fr Wrote: May be I have bad understood your question ? You said the corrected code starts with... Code: LBL'SDM' This fills up any entered number with zeroes: 12345 ENTER LOG IP 9 – 10^x / yields 1234500000 123 ENTER LOG IP 9 – 10^x / yields 1230000000 etc. This of course does not change the digit sum. 123 mod 9 = 6 1230000000 mod 9 = 6 1234 mod 9 = 1 1234000000 mod 9 = 1 So what is the reason behind this? As far as I can see the first six lines after the label are completely unnecessary. What am I missing here? Dieter RE: Sum of Digits. - BarryMead - 03-30-2016 07:54 PM (03-30-2016 12:46 PM)Dieter Wrote:This solution is very impressive. Since the SIGN function returns 3 values -1 for negative +1 for positive and Zero for Zero it also corrects the X=0 case in addition to fixing the negative value cases. Thanks RE: Sum of Digits. - ggauny@live.fr - 03-31-2016 07:33 AM Dieter, You are right of course, my routine was part of an old program from my HP41cx and I have not see the problem, Of course the 7 steps after the label are not necessary ! Code:
Thanks ! (And apologise). RE: Sum of Digits. - Dieter - 03-31-2016 12:50 PM (03-31-2016 07:33 AM)ggauny@live.fr Wrote: Now simply replace the last steps with X=0? X<> L and the code is shorter and has a common exit point. Dieter |