Sum of Digits.
|
03-25-2016, 11:16 AM
Post: #1
|
|||
|
|||
Sum of Digits.
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* Gérard. |
|||
03-25-2016, 01:41 PM
Post: #2
|
|||
|
|||
RE: Sum of Digits.
(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 |
|||
03-25-2016, 04:37 PM
Post: #3
|
|||
|
|||
RE: Sum of Digits.
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. |
|||
03-25-2016, 05:46 PM
Post: #4
|
|||
|
|||
RE: Sum of Digits.
Hi,
Very thanks for your encouragements and compliments. And yours tricks ! Gérard. |
|||
03-25-2016, 06:34 PM
(This post was last modified: 03-25-2016 08:34 PM by Dieter.)
Post: #5
|
|||
|
|||
RE: Sum of Digits.
(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 |
|||
03-25-2016, 07:44 PM
Post: #6
|
|||
|
|||
RE: Sum of Digits.
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 ! Gérard. |
|||
03-25-2016, 08:26 PM
Post: #7
|
|||
|
|||
RE: Sum of Digits.
May be, if register X is greather then 9, XEQ the routine again ?
#009 X<Y ? XEQ 'SD' END Am-I right ? Gérard. |
|||
03-25-2016, 08:33 PM
(This post was last modified: 03-25-2016 08:37 PM by Dieter.)
Post: #8
|
|||
|
|||
RE: Sum of Digits.
(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 |
|||
03-25-2016, 08:37 PM
Post: #9
|
|||
|
|||
RE: Sum of Digits.
You are diabolic and I am on the grill, but I will find, you will see !
Regards. Gérard. |
|||
03-25-2016, 08:46 PM
Post: #10
|
|||
|
|||
RE: Sum of Digits.
FP
SDL 001 + ? Gérard. |
|||
03-25-2016, 08:48 PM
Post: #11
|
|||
|
|||
RE: Sum of Digits.
(03-25-2016 07:44 PM)ggauny@live.fr Wrote: What is the meaning of : SCNR, I dont' see on "urban speaking". see here. :) Greetings, Massimo -+×÷ ↔ left is right and right is wrong |
|||
03-25-2016, 08:48 PM
Post: #12
|
|||
|
|||
RE: Sum of Digits. | |||
03-25-2016, 08:49 PM
Post: #13
|
|||
|
|||
RE: Sum of Digits.
No, not good. Because if the result is for example 156, not work.
I need to more reflexion. Gérard. |
|||
03-25-2016, 08:59 PM
(This post was last modified: 03-25-2016 09:00 PM by Dieter.)
Post: #14
|
|||
|
|||
RE: Sum of Digits. | |||
03-26-2016, 07:52 AM
Post: #15
|
|||
|
|||
RE: Sum of Digits.
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. Gérard. |
|||
03-26-2016, 07:54 AM
Post: #16
|
|||
|
|||
RE: Sum of Digits.
Hi,
Thank you Massimo ! Gérard. |
|||
03-26-2016, 01:02 PM
(This post was last modified: 03-26-2016 02:28 PM by fhub.)
Post: #17
|
|||
|
|||
RE: Sum of Digits.
(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 |
|||
03-26-2016, 01:17 PM
Post: #18
|
|||
|
|||
RE: Sum of Digits.
(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. Gérard. |
|||
03-26-2016, 06:02 PM
(This post was last modified: 03-26-2016 06:16 PM by Dieter.)
Post: #19
|
|||
|
|||
RE: Sum of Digits.
(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 |
|||
03-26-2016, 06:16 PM
(This post was last modified: 03-26-2016 06:28 PM by Dieter.)
Post: #20
|
|||
|
|||
RE: Sum of Digits.
(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 |
|||
« Next Oldest | Next Newest »
|
User(s) browsing this thread: 5 Guest(s)