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 |
Sum of Digits. - ggauny@live.fr - 03-25-2016 11:16 AM Hi all friends, Playing with my WP34s, as each days, I was thinking that, in number theory, often we need know what is the sum of digits presents in register X. So, based on an pretty old routine from John Kennedy, I would like submit you this little routine. Be aware it is not good for "perfects numbers", I am testing another routine for this special case. But it is may be helpfull for someone. Here we go : Code:
For example, if you type : 123 ==> 6 31416==>15 Hoping servicing you STO[times] = STO* RE: Sum of Digits. - Dieter - 03-25-2016 01:41 PM (03-25-2016 11:16 AM)ggauny@live.fr Wrote: Playing with my WP34s, as each days, I was thinking that, in number theory, Here is one that uses only the stack: Code: 001 LBL'SD' But I see you had time to improve your programming skills on the 34s. This also shows up in the reverse digits program. ;-) Dieter RE: Sum of Digits. - Didier Lachieze - 03-25-2016 04:37 PM Nice programs, here is a shorter version that keeps the stack unchanged excepted for X replaced by it's sum of digits. Code: 001 LBL'SD' Notes: Local registers allocated by LocR are initialized to 0, so no need to clear them. Division and multiplication by a power of 10 can be replaced by the single step instructions SDR and SDL. RE: Sum of Digits. - ggauny@live.fr - 03-25-2016 05:46 PM Hi, Very thanks for your encouragements and compliments. And yours tricks ! RE: Sum of Digits. - Dieter - 03-25-2016 06:34 PM (03-25-2016 05:46 PM)ggauny@live.fr Wrote: Very thanks for your encouragements and compliments. And yours tricks ! Yes, I also like Didier's solution. Did I already say that I'm a big fan of SDL and SDR? But now for the next challenge – the sum of digits of the sum of digits: 12345 => 15 => 6 Any solution less than four steps (!) (w/o LBL and END) is accepted. SCNR, Dieter RE: Sum of Digits. - ggauny@live.fr - 03-25-2016 07:44 PM Well, I am going to reflexion this night (I bad sleep), but in less 4 steps ! It seem very hard. I will find. What is the meaning of : SCNR, I dont' see on "urban speaking". Good night and thanks for the challenge. It is really a pleasure to learn with all of you greats programmers ! RE: Sum of Digits. - ggauny@live.fr - 03-25-2016 08:26 PM May be, if register X is greather then 9, XEQ the routine again ? #009 X<Y ? XEQ 'SD' END Am-I right ? RE: Sum of Digits. - Dieter - 03-25-2016 08:33 PM (03-25-2016 08:26 PM)ggauny@live.fr Wrote: May be, if register X is greather then 9, XEQ the routine again ? "SD" also works for x≤9. But your proposed code does not work as it returns SD(9) = 9 for any x>9, and 9 for the rest. In other words, it will always return ...9. OK, you could even do it in two lines: XEQ SD XEQ SD But that's not the real solution as it requires an external program with a significant number of steps. Actually the solution is much, much simpler, and of course it does not require calling an external program. ;-) Dieter RE: Sum of Digits. - ggauny@live.fr - 03-25-2016 08:37 PM You are diabolic and I am on the grill, but I will find, you will see ! Regards. RE: Sum of Digits. - ggauny@live.fr - 03-25-2016 08:46 PM FP SDL 001 + ? RE: Sum of Digits. - Massimo Gnerucci - 03-25-2016 08:48 PM (03-25-2016 07:44 PM)ggauny@live.fr Wrote: What is the meaning of : SCNR, I dont' see on "urban speaking". see here. :) RE: Sum of Digits. - Dieter - 03-25-2016 08:48 PM (03-25-2016 08:46 PM)ggauny@live.fr Wrote: FP ?!? How is this supposed to work? Dieter RE: Sum of Digits. - ggauny@live.fr - 03-25-2016 08:49 PM No, not good. Because if the result is for example 156, not work. I need to more reflexion. RE: Sum of Digits. - Dieter - 03-25-2016 08:59 PM (03-25-2016 08:49 PM)ggauny@live.fr Wrote: No, not good. Because if the result is for example 156, not work. It won't work for any number as the initial FP yields 0 for any integer... #-) By the way, in this case the program should return 156 => 12 => 3. Dieter RE: Sum of Digits. - ggauny@live.fr - 03-26-2016 07:52 AM hi, Ich gebe meine Zunge , um die Katze ! I give my tongue to the cat ! Je donne ma langue au chat ! I have really no solution for this challenge. Good Easter for all. RE: Sum of Digits. - ggauny@live.fr - 03-26-2016 07:54 AM Hi, Thank you Massimo ! RE: Sum of Digits. - fhub - 03-26-2016 01:02 PM (03-25-2016 06:34 PM)Dieter Wrote: But now for the next challenge – the sum of digits of the sum of digits: Well, not less than 4, but exactly 4 steps (I guess this is what you meant?): Code:
Edit: replaced MOD by RMDR, so it also works for number 0. Franz RE: Sum of Digits. - ggauny@live.fr - 03-26-2016 01:17 PM (03-25-2016 08:59 PM)Dieter Wrote:(03-25-2016 08:49 PM)ggauny@live.fr Wrote: No, not good. Because if the result is for example 156, not work. Dieter said it is not good, 156 give 3. But may be you are right, I dont' know. I think it is very difficult to answer for me. RE: Sum of Digits. - Dieter - 03-26-2016 06:02 PM (03-26-2016 01:02 PM)fhub Wrote: Well, not less than 4, but exactly 4 steps (I guess this is what you meant?): Usually I mean what I say. ;-) Four steps is easy – one example is your solution, another one is... Code: #009 Well, at least for x>0. ;-) So four steps is trivial. For less it takes some real art of programming. No, I do not have a three-step-solution either. Which does not mean it doesn't exist. ;-) Dieter RE: Sum of Digits. - Dieter - 03-26-2016 06:16 PM (03-26-2016 01:17 PM)ggauny@live.fr Wrote: But may be you are right, I dont' know. The solution is quite easy. Take a look here (Français) or here (English) or here (Deutsch). That's why checking whether a number is divisible by 3 is so easy: just add its digits and check if that sum can be divided by 3. The sum is the remainder of a division by 9. So if this remainder is 0, 3 or 6 the number can also be divided by 3. Dieter |